小编典典

在.NET 2.0中将位图转换为一个多页TIFF图像

c#

如何将位图数组转换为TIFF格式的全新图像,并将所有位图添加为该新的tiff图像中的帧?

使用.NET 2.0。


阅读 208

收藏
2020-05-19

共1个答案

小编典典

通过将第一个位图放入Image对象开始

Bitmap bitmap = (Bitmap)Image.FromFile(file);

将位图以tiff格式保存到内存中

MemoryStream byteStream = new MemoryStream();
bitmap.Save(byteStream, ImageFormat.Tiff);

将Tiff放入另一个Image对象

Image tiff = Image.FromStream(byteStream)

准备编码器:

var encoderInfo = ImageCodecInfo.GetImageEncoders().First(i => i.MimeType == "image/tiff");

EncoderParameters encoderParams = new EncoderParameters(2);
encoderParams.Param[0] = new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionNone);
encoderParams.Param[1] = new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.MultiFrame);

保存到文件:

tiff.Save(sOutFilePath, encoderInfo, encoderParams);

对于后续页面,请准备编码器:

EncoderParameters EncoderParams = new EncoderParameters(2);
EncoderParameter SaveEncodeParam = new EncoderParameter(
     Encoder.SaveFlag, 
     (long)EncoderValue.FrameDimensionPage);
EncoderParameter CompressionEncodeParam = new EncoderParameter(
     Encoder.Compression, (long)EncoderValue.CompressionNone);
EncoderParams.Param[0] = CompressionEncodeParam;
EncoderParams.Param[1] = SaveEncodeParam;
tiff.SaveAdd(/* next image as tiff - do the same as above with memory */, EncoderParams);

最后刷新文件:

EncoderParameter SaveEncodeParam = new EncoderParameter(
     Encoder.SaveFlag, (long)EncoderValue.Flush);
EncoderParams = new EncoderParameters(1);
EncoderParams.Param[0] = SaveEncodeParam;
tiff.SaveAdd(EncoderParams);

那应该让您开始。

2020-05-19