日期:2014-05-19  浏览次数:20902 次

怎么把一个Bitmap保存到单色bmp文件?
如题,注意是保存到单色的,貌似piexlformat里面没有单色的这种格式,只有个单色索引什么的。

------解决方案--------------------
仅供参考。

//
private static ImageCodecInfo GetEncoderInfo(string mimeType)
{
int j;
ImageCodecInfo[] encoders;

encoders = ImageCodecInfo.GetImageDecoders();
for (j = 0; j < encoders.Length; j++)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}

//
Image oldImage = (Image)Image.FromFile(@ "c:\test.bmp ").Clone();

Image image = new Bitmap(2304, 3307); //图片大小:2304*3307

Graphics g = Graphics.FromImage(image);

g.DrawImage(oldImage, new Rectangle(0, 0, image.Width, image.Height), cropRect.X*5, cropRect.Y*5, cropRect.Width*5, cropRect.Height*5, GraphicsUnit.Pixel);
// 这里用到的cropRect是当时我的应用中需要的东东,你自己参考g.DrawImage做调整。

ImageCodecInfo icf = GetEncoderInfo( "image/tiff ");

System.Drawing.Imaging.Encoder myEncoder;
EncoderParameter myEncoderParameter;
EncoderParameters myEncoderParameters;

myEncoder = System.Drawing.Imaging.Encoder.Compression;

myEncoderParameters = new EncoderParameters(3);
myEncoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth, 1L);
myEncoderParameters.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionCCITT4);
myEncoderParameters.Param[2] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.Flush);

image.Save( "c:\\newx.tif ", icf, myEncoderParameters);
//转换到tif或bmp,在这里不重要,关键是上面这些代码段,你可能要用上。