使用 .Net 如何用新图像替换多页 tiff 文件的第一页。最好不创建新文件。
问问题
3974 次
2 回答
1
我认为如果不创建另一个文件,您将无法做到这一点。
您可以先读取所有图像,替换要替换的图像,关闭原始源,然后将文件替换为新的多页 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页面有很多好东西。
于 2009-11-23T19:41:19.723 回答
0
这很容易做到。这个CodeProject 教程页面上有源代码,可以帮助你做你想做的事。
本质上,您需要调用Image.GetFrameCount(),它会为您提供多页 TIFF 中的图像数量(只是为了确认您实际上有一个多页 TIFF)。
您可能需要尝试如何保存生成的 TIFF - 您可能需要手动重新组装 TIFF,或者您可以在将 TIFF 写回磁盘之前直接编辑/替换图像。
于 2009-11-23T19:12:24.353 回答