开发者

C# - How to convert an Image into an 8-bit color Image?

开发者 https://www.devze.com 2022-12-12 10:34 出处:网络
I need to convert a PNG Image loaded from a file into an 8 bit-per-pixel byte array used by another device (embedded programmi开发者_JAVA百科ng).

I need to convert a PNG Image loaded from a file into an 8 bit-per-pixel byte array used by another device (embedded programmi开发者_JAVA百科ng).

I'm copying over pixel by pixel into a new Image created with a 16-bit color - Format16bppRgb565, but I need Format8bpp, which is not present in the .NET Framework. (I cannot use Format8bppIndexed since the other device cannot handle indexed bitmaps)

 Bitmap img = new Bitmap(imgPath);
 Bitmap img8 = new Bitmap(imgW, imgH, PixelFormat.Format16bppRgb565);
 for (int I = 0; I <= img.Width - 1; I++) {
     for (int J = 0; J <= img.Height - 1; J++) {
         img8.SetPixel(I, J, img.GetPixel(I, J));
     }
 }

So how can I convert an Image into an 8-bit per pixel byte array?

  • Can I use the 8-bit indexed format and extract the bytes? - Format8bppIndexed
  • Can I calculate the pixel colors by reducing the 32-bit color R,G,B values to 8-bit?
  • Is there a C# library that does this already?
  • "The easiest way is to create a new 8 bit bitmap, and draw your 24 bit image onto it." - how?


Although I'm not sure how to do it with plain vanilla C# you can use the FreeImage Project libraries to convert images formats:

FreeImage is an Open Source library project for developers who would like to support popular graphics image formats like PNG, BMP, JPEG, TIFF and others as needed by today's multimedia applications. FreeImage is easy to use, fast, multithreading safe, compatible with all 32-bit versions of Windows, and cross-platform (works both with Linux and Mac OS X).

It's written in C++ but has good .NET wrappers you can use.


I already did it in one Pattern Recognition project. Searching some image processing and pattern recognition books you can find these solutions.

a) If you want to convert to greyscale you can find in general two solutions.

#1 Averaging RGB channels. - You can take 8-bit values for avery single channel and calculate pixel = ( R+G+B )/3

#2 Take only the Green component. It is best viewed.

b) If you want to convert to 8bit color representation you need to use a Color table. You can see a solution here http://www.codeproject.com/KB/graphics/Image_Bitdepth_Conversion.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消