32bit rgb bmp to 24bit rgb bmp in C# problem

☆樱花仙子☆ 提交于 2019-12-10 12:06:20

问题


I have to convert 32bit rgb bmp images in to 24 bits rgb bmp.

This is what i am trying to do

Bitmap b1=new Bitmap(sorecFileName);

Bitmap b2=new 

Bitmap(b1.Size.Width,b1.Size.Height,System.Drawing.Imaging.PixelFormat.Format24bppRgb);       
b2.SetResolution(b1.HorizontalResolution, b1.VerticalResolution);

Graphics g=Graphics.FromImage(b2);

g.DrawImage(b1,0,0);

//continue to draw on g here to add text or graphics.

g.Dispose();

b2.Save(destinationFileName);

The code compiles fine and generates the output image of 24bpp but its not in the rgb format any more. Why is this so?

I figured it out as I have a library that takes input of an image as rgb24 and displays it. So when I try to give the file generated by above code as input to the function, it displays noisy image.

However, if I open the same file in paint and save it as 24bpp bmp, and input it to the function, the picture displays fine. What am I missing?


回答1:


  b2.Save(destinationFileName);

You didn't specify the image file format. The default is PNG, not BMP. You now probably have a .bmp file on disk that actually contains a PNG image. That can go undetected for quite a while, lots of graphics programs pay attention to the file header instead of the file name extension. MSPaint for example will have no trouble loading the file. Just like any other program that uses GDI+. You might not be so lucky with a program that blindly assumes that the file contains a BMP and does no checking at all. Fix:

  b2.Save(destinationFilename, System.Drawing.Imaging.ImageFormat.Bmp);



回答2:


if you got a blackimage - Add g.Clear(Color.White);



来源:https://stackoverflow.com/questions/4967937/32bit-rgb-bmp-to-24bit-rgb-bmp-in-c-sharp-problem

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!