Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#问题中的32位rgb bmp到24位rgb bmp_C#_Imaging - Fatal编程技术网

C#问题中的32位rgb bmp到24位rgb bmp

C#问题中的32位rgb bmp到24位rgb bmp,c#,imaging,C#,Imaging,我必须将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

我必须将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,然后将其输入到函数中,则图片显示良好。我错过了什么

  b2.Save(destinationFileName);
您没有指定图像文件格式。默认值是PNG,而不是BMP。现在,磁盘上可能有一个.bmp文件,其中实际上包含一个PNG图像。这可能会在相当长的一段时间内不被发现,许多图形程序关注的是文件头而不是文件扩展名。例如,MSPaint加载文件不会有问题。就像其他任何使用GDI+的程序一样。如果一个程序盲目地假设文件包含BMP并且根本不进行检查,那么您可能就不会这么幸运了。修正:

  b2.Save(destinationFilename, System.Drawing.Imaging.ImageFormat.Bmp);
如果你有一张黑影-
添加g.Clear(颜色为白色)

请改进此库中的错误检查。像这样的灾难并不罕见,总有一天会再次发生。。。万分感谢,这正是我错过的