473,385 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Display bmp in picturebox

16
Expand|Select|Wrap|Line Numbers
  1.  PictureBox PictureBox1 = new PictureBox();
  2.         public void display(String filename)
  3.         {
  4.             FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
  5.             BinaryReader br = new BinaryReader(fs);
  6.             if (br.ReadChar().ToString() + br.ReadChar().ToString() == "BM")
  7.             {
  8.                 int bfSize = br.ReadInt32();
  9.                 int bfReserved1 = br.ReadInt16();
  10.                 int bfReserved2 = br.ReadInt16();
  11.                 int bfoffbits = br.ReadInt32();
  12.                 int biSize = br.ReadInt32();
  13.                 int biWidth = br.ReadInt32();
  14.                 int biHeight = br.ReadInt32();
  15.                 int biPlanes = br.ReadInt16();
  16.                 int biBitCount = br.ReadInt16();
  17.                 int biCompression = br.ReadInt32();
  18.                 int biSizeImage = br.ReadInt32();
  19.                 int biXPelsPerMeter = br.ReadInt32();
  20.                 int biYPelsPerMeter = br.ReadInt32();
  21.                 int biClrUsed = br.ReadInt32();
  22.                 int biClrImportant = br.ReadInt32();
  23.                 int rgbRed = 0;
  24.                 int rgbGreen = 0;
  25.                 int rgbBlue = 0;
  26.                 Bitmap bmp = new Bitmap(biWidth, biHeight);
  27.                  PictureBox1.Size = new System.Drawing.Size(biWidth, biHeight);
  28.  
  29.                 try
  30.                 {
  31.                     for (int y = biHeight - 1; y >= 0; y--)
  32.                     {
  33.                         for (int x = 0; x < biWidth; x++)
  34.                         {
  35.                                                     }
  36.                     }
  37.  
  38.                    PictureBox1.Image = bmp;
  39.                 }
  40.                 catch
  41.                 {
  42.                     MessageBox.Show("there is some problem with file format");
  43.                 }
  44.             }
  45.             else
  46.                 MessageBox.Show("this is not a bitmap file");
  47.             fs.Flush();
  48.         }
HI EVERYONE
I WANT TO GET BMP BY ITS HEDER INFORMATION THEN DISPLAY IT, THIS CODE RUNNING BUT DON'T WORK, ANYONE CAN HELP ME PLZ
Mar 28 '11 #1
18 5826
GaryTexmo
1,501 Expert 1GB
Ease up on the caps lock ;)

Can you please describe in a little more detail how it's not working? You mention it runs, but all you said is that it doesn't work. Does the image display incorrectly, or not at all? Is there an exception being thrown?

Post a bit more information and we'll see what we can do to help you out :)

Oh also, relax! It's easy to get worked up over a problem... it happens to me all the time. Try to step back and try to clear your head, maybe do something else for a while, then come back to it when you're more come. Sometimes that's all you need and things that have been eluding you become more clear.
Mar 28 '11 #2
saharit
16
it's running but only get the path of file but don't display picture,sometimes say "this is not a bitmap file" .
i should get bmp file by its heder information then display it pixel-by-pixel
i have note enough time ,:(
thanks
Mar 28 '11 #3
GaryTexmo
1,501 Expert 1GB
Well, your program outputs that message... it looks like it's the else condition to the following if block:

Expand|Select|Wrap|Line Numbers
  1. if (br.ReadChar().ToString() + br.ReadChar().ToString() == "BM")
Try this...

Expand|Select|Wrap|Line Numbers
  1. string headerCode = br.ReadChar().ToString() + br.ReadChar().ToString();
  2. Console.WriteLine(headerCode);
  3. if (headerCode == "BM") ...
See what it's reading out there. It's obviously not "BM" but that should let you see what it is. Note, you might either want to inspect it via a breakpoint or output the int values of the string too (in case the characters that come out aren't letters... like, if it's a newline you won't be able to see it, but it's there).
Mar 28 '11 #4
saharit
16
thanks its work, i have another problem .
how to display bmp file pixel-by-pixel and line-by-line without buffering it? :)
Mar 28 '11 #5
GaryTexmo
1,501 Expert 1GB
I'm not sure what you mean by that, can you clarify please?
Mar 28 '11 #6
saharit
16
in this code at first we buffered the bmp then display it . i want to Simultaneous that we read the bmp, display it .
Mar 28 '11 #7
GaryTexmo
1,501 Expert 1GB
Oh, you want to display it as it's being read out, so a large image would draw as it loaded? You don't want it to have a large delay, then draw the whole thing, right?

The way you're making the image, ie making a Bitmap object and filling it with data, you could probably draw it mid-load and it would be just fine. To that end, maybe you can put the loading of the bitmap data into some kind of background worker, then periodically refresh your main form on a timer which would just redraw the image based on it's current state?

If you do that, make sure you lock/unlock the image. I'm not sure if that's the exact approach, but it's something to look into. You might as well give it a try :)
Mar 28 '11 #8
saharit
16
thanks a lot, i'm going to try what you say:)
Mar 29 '11 #9
saharit
16
do u know why this code don't work?


Expand|Select|Wrap|Line Numbers
  1.   private void Form1_Load(object sender, EventArgs e)
  2.  
  3. {
  4.  
  5. OpenFileDialog ofd = new OpenFileDialog();
  6. ofd.ShowDialog();
  7.  
  8.  
  9. FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read);
  10.  
  11. BinaryReader br = new BinaryReader(fs);
  12.  
  13. if (br.ReadChar().ToString() + br.ReadChar().ToString() == "BM")
  14.  
  15. {
  16.  
  17. int size = br.ReadInt32();
  18.  
  19. br.ReadInt16(); //Reserved
  20.  
  21. br.ReadInt16(); //Reserved
  22.  
  23. int offsetToBitmap = br.ReadInt32();
  24.  
  25. int bytesInStruct = br.ReadInt32();
  26.  
  27. int width = br.ReadInt32();
  28.  
  29. int height = br.ReadInt32();
  30.  
  31. short planes = br.ReadInt16();
  32.  
  33. short bitcount = br.ReadInt16();
  34.  
  35. int compression = br.ReadInt32();
  36.  
  37. int imagesize = br.ReadInt32();
  38.  
  39. br.ReadInt32(); //Reserved
  40.  
  41. br.ReadInt32(); //Reserved
  42.  
  43. int colorsused = br.ReadInt32();
  44.  
  45. int importantColors = br.ReadInt32();
  46.  
  47. Bitmap bmp = new Bitmap(width, height);
  48.  
  49. pictureBox1.Size = new Size(width, height);
  50.  
  51. byte[] pixels = br.ReadBytes(3 * width * height);
  52.  
  53. for (int p = pixels.Length - 1; p > 0; p -= 3)
  54.  
  55. {
  56.  
  57. Point a = GetXY(Convert.ToInt32((p + 1) / 3), width, height);
  58.  
  59. bmp.SetPixel(a.X, a.Y, Color.FromArgb(pixels[p], pixels[p - 1], pixels[p - 2]));
  60.  
  61. }
  62.  
  63. pictureBox1.Image = bmp;
  64.  
  65. }
  66.  
  67. //
  68.  
  69. fs.Flush();
  70.  
  71.  
  72.  
  73. }
  74.  
  75. private Point GetXY(int n, int width, int height)
  76.  
  77. {
  78.  
  79. int x = 0;
  80.  
  81. int y = 0;
  82.  
  83. int n2 = n;
  84.  
  85. while (n - width > 0)
  86.  
  87. {
  88.  
  89. n -= width;
  90.  
  91. y++;
  92.  
  93. }
  94.  
  95. while (n2 - height > 0)
  96.  
  97. {
  98.  
  99. n2 -= height;
  100.  
  101. }
  102.  
  103. x = n2 - 1;
  104.  
  105. return new Point(width - 1 - x, height - 1 - y);
  106.  
  107. }
Apr 8 '11 #10
GaryTexmo
1,501 Expert 1GB
Can you please describe the problem in more detail?
Apr 8 '11 #11
saharit
16
do u know how i get bmp header and put pixel by pixel in arry then display it without using bitmap class, i don't have enough time :(
Apr 12 '11 #12
GaryTexmo
1,501 Expert 1GB
I don't off hand, no... last time I had to read an image format I googled the specs and went to work. That said, I'd imagine that's how you got as far as you have, so perhaps you should describe what your code is doing and how it's not working, then maybe we can help you get to the bottom of it.

Also, regarding your time limit, it's worth pointing out that Bytes is not a place for you to get your assignments/work done for you. We can help you work through problems and understand concepts, but at the end of the day your work is yours to do. I'm sorry if we haven't been able to help you thus far, but please keep in mind that ultimately, the responsibility to have the work done on time is yours.

If you still need help on this issue, please post more details regarding the problem your code is having. How it doesn't work, what problems it might be generating. Any errors or exceptions, etc...
Apr 12 '11 #13
saharit
16
in first code i use this befor the first loop>> int padding = biWidth % 4;<< and add this in the next loop >> fs.Seek(padding, SeekOrigin.Current);<< beacuse the picture is shown Tilt .do u know why i could Division the biwidth on 4 ?
Apr 13 '11 #14
GaryTexmo
1,501 Expert 1GB
http://en.wikipedia.org/wiki/BMP_file_format

It looks like the row itself is padded to a multiple of 4 bytes, not each pixel. I'm not sure if that helps you.

Some good information there though. Go over your code using this wikipedia sheet and make sure you're reading the offsets properly. If you're still having trouble, maybe I'll take a crack at it and see if I have any luck.

At this point though, I gotta ask. Why can't you use the Image.FromFile method?
Apr 13 '11 #15
saharit
16
At this point though, I gotta ask. Why can't you use the Image.FromFile method?
>>> where i can use it?
Apr 13 '11 #16
GaryTexmo
1,501 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. Bitmap b = Image.FromFile("an_image.bmp");
The only reason I didn't suggest it earlier is you seemed pretty adamant that you read the image manually. You seemed to suggest it was a requirement to read and parse the file.

Haha hopefully that's all you need after all :)

*Edit: I read back over your stuff and yea, you mentioned you want to read and show the bitmap simultaneously. Using Image.FromFile, it will completely buffer it then display it. If you still want what you laid out at the start, you'll likely have to continue with what you've started above.
Apr 13 '11 #17
saharit
16
hi again ,
1)i have problem with reading bmp file in 8 bit,do u know how can i load it in my program?

2) i still don't undrestand why the padding is
int padding = biWidth % 4;
:(
May 21 '11 #18
GaryTexmo
1,501 Expert 1GB
Hello,

1) I'm afraid I don't know the answer to that question. The current standard for the bitmap format is 32-bit or 24-bit, I don't know how an 8-bit bitmap works. Looking at the doc, it looks like it maps to a colour table. Read the wiki link above. If you're looking for a way to load it with Image.FromFile, if it doesn't do it automatically you might have to load it yourself after all.

2) Because that's what the format requires. I believe they wanted to make sure there was always the same amount of data on each row. I don't know the intricacies, that's just what someone decided on :)
May 24 '11 #19

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: pcPirate | last post by:
Hi, I have a control PictureBox and I need to display a picture from Sql Database field, given data type Image. i) How should I diplay the picture in the PictureBox from the database? ii) How...
1
by: James Dean | last post by:
I am drawing in a picturebox and am having some problems. I display outputted images in the picturebox. If the image is too big for the client area then i have scrollbars. The problem is that once...
2
by: active | last post by:
Problem: The PictureBox display appears to have the image cut off. I.e., the image bottom does not display although the PictureBox has room for it. It occurred to me that what was displayed was...
5
by: BrianW | last post by:
I am working on a program that has multiple picturebox controls that a user is allowed to move around which are contained within a panel control for visual placement. In my mousedown event, I set...
3
by: Tom | last post by:
I have a picturebox on my VB.NET form. The picturebox size mode is set to stretched. I then load an image into that form and display it. As the user moves the mouse over the form, I want to get and...
6
by: Rich | last post by:
Hello, I want to simulate the dynamic thumbnail display of Windows Explorer (winxp) on a form or pannel container. If I place a picture box on my container form/pannel and dimension it to the...
4
by: TomA | last post by:
Hi All, I have a picturebox on a form containing the photo of a person. As you advance through the records, the photo updates. Rather than storing the images in an inefficient blob field in a...
3
by: Andrzej | last post by:
I have a picturebox on my C# .NET form. The picturebox size mode is set to zoom. I then load an image into that form and display it. As the user moves the mouse over the form, I want to get and...
1
by: nanaalwi | last post by:
Hi, Currently I'm developing a software using VB.net that can grab an image using Matrox Morphis card and display it in a PictureBox. The software can grab the image already and display it in the...
0
by: swagatikasahoo | last post by:
how to display all lines if i will scroll the picturebox. hi, ok i m sending the code. ****************************************************** Private Sub PictureBox1_Paint(ByVal sender As Object,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.