至少可以说,使用 System.Drawing 处理多页 TIF 可能很棘手。这可能就是为什么有专门的图像库包含更精细的 TIFF 支持的原因。以下代码可能不是您特定问题的理想解决方案,但在使用具有 1 位、8 位和 24 位页面的 TIFF 文件进行测试时,它可以工作,并将它们全部转换为 24 位。该代码创建新的 24 位页面并将它们全部附加到一个新文件中,而原始文件保持不变。
if (System.IO.File.Exists(outputTif))
System.IO.File.Delete(outputTif);
Bitmap img = new Bitmap(inputTif);
Bitmap multiPageImage = (Bitmap)img.Clone();
multiPageImage.SelectActiveFrame(FrameDimension.Page, 0);
Bitmap firstPage = new Bitmap(multiPageImage.Width, multiPageImage.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(firstPage);
g.DrawImageUnscaled(multiPageImage, 0, 0);
g.Dispose();
ImageCodecInfo TiffCodec = null;
foreach (ImageCodecInfo codec in ImageCodecInfo.GetImageEncoders())
if (codec.MimeType == "image/tiff")
{
TiffCodec = codec;
break;
}
EncoderParameters parameters = new EncoderParameters(2);
parameters.Param[0] = new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.MultiFrame);
parameters.Param[1] = new EncoderParameter(Encoder.ColorDepth, (long)24);
//save the first page in a new file
firstPage.Save(outputTif, TiffCodec, parameters);
parameters = new EncoderParameters(2);
parameters.Param[0] = new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage);
parameters.Param[1] = new EncoderParameter(Encoder.ColorDepth, (long)24);
var pageCount = multiPageImage.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page);
//now append pages from second to last
for (int i = 1; i < pageCount; ++i)
{
multiPageImage = (Bitmap)img.Clone();
multiPageImage.SelectActiveFrame(FrameDimension.Page, i);
Bitmap nextPage = new Bitmap(multiPageImage.Width, multiPageImage.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
g = Graphics.FromImage(nextPage);
g.DrawImageUnscaled(multiPageImage, 0, 0);
g.Dispose();
firstPage.SaveAdd(nextPage, parameters);
nextPage.Dispose();
}
parameters = new EncoderParameters(2);
parameters.Param[0] = new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.Flush);
parameters.Param[1] = new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.LastFrame);
firstPage.SaveAdd(parameters);
firstPage.Dispose();