使用 .Net 替换现有多页 Tiff 中的图像

发布于 2024-08-12 08:15:22 字数 47 浏览 5 评论 0原文

使用 .Net 如何用新图像替换多页 tiff 文件的第一页。最好不创建新文件。

Using .Net how do I replace the first page of a multiple page tiff file with a new image. Preferable without create a new file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苦行僧 2024-08-19 08:15:22

我认为如果不创建另一个文件你就无法做到这一点。

您可以先读取所有图像,替换要替换的图像,关闭原始源,然后用新的多页 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页面有很多好东西。

I think you could not do it without creating another file.

You could read all the images first, replace the image you want to replace, close the original source, and then replace the file with the new multi-page TIFF, but I believe it will use a lot of memory. I would read an image at a time, and write it to a new file and, as the last step, change the file names.

Something like:

 // 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... 

Hope it helps. For questions in .NET imaging, the Bob Powell page has a lot of good stuff.

阳光的暖冬 2024-08-19 08:15:22

这很容易做到。 CodeProject 教程页面上有源代码,可以帮助您执行以下操作你想要的。

本质上,您需要调用 Image .GetFrameCount() 将为您提供多页 TIFF 中的图像数量(只是为了确认您确实拥有多页 TIFF)。

您可能需要尝试如何保存生成的 TIFF - 您可能需要手动重新组装 TIFF,或者您可以在将 TIFF 写回磁盘之前直接编辑/替换图像。

This is fairly easy to do. There's source code on this CodeProject tutorial page that will help you do what you want.

Essentially, you'll need to make a call to Image.GetFrameCount() which will give you the number of images in your multi-page TIFF (just to confirm that you actually have a multi-page TIFF).

You might need to experiment with how you save your resulting TIFF - you may need to reassemble the TIFF manually, or you might be able to edit/replace the image directly before writing the TIFF back to disk.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文