【发布时间】:2009-11-23 18:58:22
【问题描述】:
使用 .Net 如何将多页 tiff 文件的第一页替换为新图像。最好不要创建新文件。
【问题讨论】:
使用 .Net 如何将多页 tiff 文件的第一页替换为新图像。最好不要创建新文件。
【问题讨论】:
我认为如果不创建另一个文件,您将无法做到这一点。
你可以先读取所有图片,替换你要替换的图片,关闭原始源,然后用新的多页TIFF替换文件,但我相信它会占用大量内存。我会一次读取一张图像,然后将其写入一个新文件,最后一步是更改文件名。
类似:
// open a multi page tiff using a Stream
using(Stream stream = // your favorite stream depending if you have in memory or from file.)
{
Bitmap bmp = new Bitmap(imagePath);
int frameCount = bmp.GetFrameCount(FrameDimension.Page);
// for a reference for creating a new multi page tiff see:
// http://www.bobpowell.net/generating_multipage_tiffs.htm
// Here all the stuff of the Encoders, and all that stuff.
EncoderParameters ep = new EncoderParameters(1);
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
Image newTiff = theNewFirstImage;
for(int i=0; i<frameCount; i++)
{
if(i==0)
{
// Just save the new image instead of the first one.
newTiff.Save(newFileName, imageCodecInfo, Encoder);
}
else
{
Bitmap newPage = bmp.SelectActiveFrame(FrameDimension.Page);
newTiff.SaveAdd(newPage, ep);
}
}
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
newTiff.SaveAdd(ep);
}
// close all files and Streams and the do original file delete, and newFile change name...
希望对您有所帮助。对于 .NET 映像方面的问题,Bob Powell 页面有很多好东西。
【讨论】:
这很容易做到。这个CodeProject tutorial page 上有源代码,可以帮助你做你想做的事。
基本上,您需要致电 Image.GetFrameCount(),它会为您提供多页 TIFF 中的图像数量(只是为了确认您确实拥有多页 TIFF)。
您可能需要尝试如何保存生成的 TIFF - 您可能需要手动重新组装 TIFF,或者您可以在将 TIFF 写回磁盘之前直接编辑/替换图像。
【讨论】: