C#中32位rgb bmp到24位rgb bmp问题
我必须将 32 位 RGB BMP 图像转换为 24 位 RGB BMP。
这就是我想要做的
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);
代码编译良好并生成 24bpp 的输出图像,但它不再是 rgb 格式。为什么会这样呢?
我发现了这一点,因为我有一个库,它将图像输入作为 rgb24 并显示它。因此,当我尝试将上述代码生成的文件作为函数的输入时,它会显示嘈杂的图像。
但是,如果我在 Paint 中打开相同的文件并将其另存为 24bpp bmp,并将其输入到该函数中,则图片显示正常。我缺少什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有指定图像文件格式。默认为 PNG,而不是 BMP。现在,您的磁盘上可能有一个 .bmp 文件,其中实际上包含一个 PNG 图像。这可能会在相当长一段时间内未被检测到,许多图形程序都会关注文件头而不是文件扩展名。例如,MSPaint 加载文件不会有任何问题。就像使用 GDI+ 的任何其他程序一样。对于盲目地假设文件包含 BMP 并且根本不进行检查的程序,您可能不会那么幸运。使固定:
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:
如果你有一张黑色图像 -
添加 g.Clear(Color.White);
if you got a blackimage -
Add g.Clear(Color.White);