简体   繁体   English

从内存流与文件流写入多页 tif 文件?

[英]Writing a multipaged tif file from memorystream vs filestream?

I am trying to use a LibTiff.Net library and rewriting a merge tool TiffCP api to use memory streams.我正在尝试使用LibTiff.Net库并重写合并工具TiffCP api 以使用内存流。

This library has a Tiff class and by passing a stream to this class, it can merge tiff images into this stream.这个库有一个Tiff类,通过向这个类传递一个流,它可以将 tiff 图像合并到这个流中。

For testing, I passed on a Filestream and I got what i wanted - it merged and I was able to see multipage tif.为了测试,我通过一个Filestream ,我得到了我想要的-它合并了,我能看到多页TIF。

But when I pass a MemoryStream , I am able to verify that the page data is being added to the stream as I loop through but when I write it to the file at the end, I could see only 1st page.但是,当我传递MemoryStream ,我能够验证页面数据是否在循环时添加到流中,但是当我最后将其写入文件时,我只能看到第一页。

var mso = new MemoryStream();
var fso = new FileStream(@"C:\test\ttest.tif",FileMode.OpenOrCreate); //This works
using (Tiff outImage = Tiff.ClientOpen("custom", "w", mso, tso))
{
 //...
 //..
 System.Drawing.Image tiffImg = System.Drawing.Image.FromStream(mso, true);
 tiffImg.Save(@"C:\test\test2.tiff", System.Drawing.Imaging.ImageFormat.Tiff);
 tiffImg.Dispose();
 //..
 //..
}

PS: I need it in memorystream because, of some folder permissions on servers + vendor API reasons. PS:我需要在内存流中使用它,因为服务器上的某些文件夹权限+供应商 API 的原因。

You probably using the memory stream before data is actually written into the stream.您可能在数据实际写入流之前使用了内存流。

Please use Tiff.Flush() method before accessing data in the memory stream.请在访问内存流中的数据之前使用Tiff.Flush()方法。 And please make sure you call Tiff.WriteDirectory() method for each page you create.并且请确保为您创建的每个页面调用Tiff.WriteDirectory()方法。

EDIT:编辑:

Please also take a look at Bob Powell's article on Generating Multi-Page TIFF files .另请查看 Bob Powell 关于生成多页 TIFF 文件的文章。 The article shows how to use EncoderParameter s to actually generate a multipage TIFF.文章展示了如何使用EncoderParameter来实际生成多页 TIFF。

Using使用

tiffImg.Save(@"C:\test\test2.tiff", System.Drawing.Imaging.ImageFormat.Tiff);

you are probably save only first frame.您可能只保存第一帧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM