如何将一组位图转换为TIFF格式的全新图像,在这个新的tiff图像中添加所有位图作为帧?
使用.NET 2.0.
从第一个位图开始,将其放入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);
这应该让你开始.
在Google上进行了一些搜索后,发现了这篇文章.我尝试了'b'c'd'e'f'g'h'在帖子中的代码,但这对我不起作用.也许,我没有正确地做某事.
在任何情况下,我发现另一个帖子将图像保存到多页tiffs.这是帖子的链接:http://www.bobpowell.net/addframes.htm
此外,这是适合我的代码.它应该与该帖子相同.
Encoder encoder = Encoder.SaveFlag; ImageCodecInfo encoderInfo = ImageCodecInfo.GetImageEncoders().First(i => i.MimeType == "image/tiff"); EncoderParameters encoderParameters = new EncoderParameters(1); encoderParameters.Param[0] = new EncoderParameter(encoder, (long)EncoderValue.MultiFrame); // Save the first frame of the multi page tiff Bitmap firstImage = (Bitmap) _scannedPages[0].RawContent; firstImage.Save(fileName, encoderInfo, encoderParameters); encoderParameters.Param[0] = new EncoderParameter(encoder, (long)EncoderValue.FrameDimensionPage); // Add the remaining images to the tiff for (int i = 1; i < _scannedPages.Count; i++) { Bitmap img = (Bitmap) _scannedPages[i].RawContent; firstImage.SaveAdd(img, encoderParameters); } // Close out the file encoderParameters.Param[0] = new EncoderParameter(encoder, (long)EncoderValue.Flush); firstImage.SaveAdd(encoderParameters);