Manually created byte array to picture box.
Hi everyone,
I'm fairly new to C#. I have created a byte array which has elements which I have manually specified. Specifically, these bytes correspond to BGR components of an image (again, I emphasise that I have manually specified the elements.
I would like to display my byte array as an image in a picture box. I've researched a lot, but have only found how to convert a byte array to a picture box image provided the byte array has been created by reading an image from file etc, but not manually created.
I figure my issue has something to do with image header information being absent.
Any help would be greatly appreciated.
Faithfully,
Antonio.
Re: Manually created byte array to picture box.
Yes, images follow a certain format, they are not simply made of pixel data. If you are going to manually create images you should either do it in code on a Bitmap object or read up on various image formats and create it correctly.
Re: Manually created byte array to picture box.
Hi,
Thanks for the clarification. So say I have the byte array that I've manually specified, and I create a new bitmap object, how would I go about correctly entering the pixel data into the bitmap object, (and header info if necessary), so that it can be displayed in a picture box.
Sincerely,
Antonio.
Re: Manually created byte array to picture box.
You don't, the library takes care of that for you. Create a bitmap object and start setting pixel values. When you save it you specify a format and .NET takes care of the rest. This is great because it saves us a ton of time, but it is unfortunate that many programmers have no clue as to how these things actually work behind the scenes.
Re: Manually created byte array to picture box.
Thanks Ed, that worked. I guess the invisible behind-the-scenes work is what makes high level programming so attractive.
Cheers.
Re: Manually created byte array to picture box.
BTW, if you ever find that Bitmap.Get/SetPixel is too slow (it is pretty slow, image analysis is tough with those) you can call LockBits on the bitmap and get a pointer to memory. Just remember that windows stores them in BGR order!
Re: Manually created byte array to picture box.
You can display a byte array with manually defined RGB values with the following code:
[code]
int width = 320;
int height = 240;
byte[] imageData = new byte[width*height*3];
// set RGB values manually
.................
.....................
// display the image within the picture box
//Here create the Bitmap to the know height, width and format
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
//Create a BitmapData and Lock all pixels to be written
BitmapData bmpData = bmp.LockBits(
new Rectangle(0, 0, bmp.Width, bmp.Height),
ImageLockMode.ReadWrite, bmp.PixelFormat);
//Copy the data from the byte array into BitmapData.Scan0
Marshal.Copy(imageData, 0, bmpData.Scan0, imageData.Length);
//Unlock the pixels
bmp.UnlockBits(bmpData);
this.m_PicView.Image = bmp;
[\code]