2

I know how to convert byte array to image. Here is my code:

//Here create the Bitmap to the know height, width and format
Bitmap bmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
//Bitmap bmp = new Bitmap(width, height);

//Create a BitmapData and Lock all pixels to be written 
System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(
                     new Rectangle(0, 0, bmp.Width, bmp.Height),
                     ImageLockMode.WriteOnly, bmp.PixelFormat);

//Copy the data from the byte array into BitmapData.Scan0
Marshal.Copy(imgDataArray, 0, bmpData.Scan0, imgDataArray.Length);
//Unlock the pixels
bmp.UnlockBits(bmpData);
bmp.Save("output1.png", System.Drawing.Imaging.ImageFormat.Png); 

But when byte array is BGR format, the image color is wrong (red is blue; blue is red). Anyone can help me to figure out that? Thanks.

1 Answer 1

2

Looks like you'll need to go through the array and swap the R/B values for each pixel. This should be fairly straightforward.

There is code in the article at http://www.codeproject.com/Articles/2056/Image-Processing-for-Dummies-with-C-and-GDI-Part-3 under the Edge Enhancement header that should do the job.

1
  • Well, any speed requirement was unspecified. It should be possible to do such a conversion very quickly. You need to write your own optimized image conversion function, or evaluate 3rd party image processing libraries until you find one that works quickly enough for your needs. Maybe you could also get your source data in another format as well.
    – djdanlib
    Apr 3, 2012 at 21:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.