我正在尝试将位图保存为jpg灰度(8位)文件,但是当我尝试使用保存方法传递文件名,ImageCodecInfo和EncoderParameters时,它会以灰度级保存图像,但是以24位,下面跟随我的代码:
using (Bitmap b = new Bitmap(f.FullName))
{
//ImageProcessor.CopyToBpp(b, 8) convert image from color to 8-bits grayscale
using (Bitmap img = bitPixel == 8 ? ImageProcessor.CopyToBpp(b, 8) : (Bitmap)b.Clone())
{
/*
* Here my img variable is a image in 8-bit (img.PixelFormat == Format8bppIndexed)
* */
string path = Path.Combine(pathToSave, string.Concat(Path.GetFileNameWithoutExtension(f.FullName), ".jpg"));
ImageCodecInfo imageCodecInfo = GetEncoder(ImageFormat.Jpeg);
EncoderParameters encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)smoothing);
img.Save(path, imageCodecInfo, encoderParameters);
img.Dispose();
}
}
即使我试图强制EncoderParameter设置ColorDepth传递值8L(8长),它仍然以24位保存文件。
有没有办法使用C#保存jpg灰度,或者我必须使用一些C ++组件?