如何使用 Bit Miracle LibTiff.Net 将图像写入 MemoryStream

发布于 2024-12-02 12:58:44 字数 1100 浏览 6 评论 0原文

我收到了使用 CCITTFaxDecode 压缩的图像。因此,我使用 Bit Miracle 的 LibTiff.Net 来将图像转换为任何格式。

我需要将解压缩的图像写入MemoryStream。我使用了另一个线程的代码示例,并且能够使用此代码。

using BitMiracle.LibTiff.Classic;
...   

MemoryStream ms = new MemoryStream();
TiffStream stm = new TiffStream();

Tiff tiff = Tiff.ClientOpen("","w",ms,stm);
tiff.SetField(TiffTag.IMAGEWIDTH, UInt32.Parse(pd.Get(PdfName.WIDTH).ToString()));
tiff.SetField(TiffTag.IMAGELENGTH, UInt32.Parse(pd.Get(PdfName.HEIGHT).ToString()));
tiff.SetField(TiffTag.COMPRESSION, Compression.CCITTFAX4);
tiff.SetField(TiffTag.BITSPERSAMPLE, UInt32.Parse(pd.Get(PdfName.BITSPERCOMPONENT).ToString()));      
tiff.SetField(TiffTag.SAMPLESPERPIXEL, 1);       
tiff.WriteRawStrip(0, raw, raw.Length);  
MemoryStream newStream =   (MemoryStream)tiff.Clientdata();

tiff.Close(); 

我遇到的问题是 MemoryStream 字节数组不是有效的图像。

我使用 System.Drawing.Image 类来加载这个 newStream 内存流,但字节数组中有一些空值。

如果我使用 Open 构造函数将图像写入磁盘,它就可以正常工作。

我想知道是否有人知道为什么 MemoryStream 无法存储解压缩的图像。

谢谢

I received a Image compressed using CCITTFaxDecode. So I used LibTiff.Net from Bit Miracle to be able to convert the image to any format.

I need to write the decompressed image to a MemoryStream. I used a code example from another thread and I was able to use this code

using BitMiracle.LibTiff.Classic;
...   

MemoryStream ms = new MemoryStream();
TiffStream stm = new TiffStream();

Tiff tiff = Tiff.ClientOpen("","w",ms,stm);
tiff.SetField(TiffTag.IMAGEWIDTH, UInt32.Parse(pd.Get(PdfName.WIDTH).ToString()));
tiff.SetField(TiffTag.IMAGELENGTH, UInt32.Parse(pd.Get(PdfName.HEIGHT).ToString()));
tiff.SetField(TiffTag.COMPRESSION, Compression.CCITTFAX4);
tiff.SetField(TiffTag.BITSPERSAMPLE, UInt32.Parse(pd.Get(PdfName.BITSPERCOMPONENT).ToString()));      
tiff.SetField(TiffTag.SAMPLESPERPIXEL, 1);       
tiff.WriteRawStrip(0, raw, raw.Length);  
MemoryStream newStream =   (MemoryStream)tiff.Clientdata();

tiff.Close(); 

The problem I'm having is that the MemoryStream byte array is not a valid image.

I used System.Drawing.Image class to load this newStream memory stream, but there are some null values in the byte array.

If I use the Open constructor to write the image to disk it works fine.

I would like to know if somebody knows why the MemoryStream fails to store the decompressed image.

Thanks

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

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

发布评论

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

评论(1

请恋爱 2024-12-09 12:58:44

问题是:

Tiff 对象在调用 Close 方法后关闭并处置流。

您可能应该更改

MemoryStream newStream = (MemoryStream)tiff.Clientdata();

MemoryStream newStream = new MemoryStream(ms.ToArray());

因此,如果稍后需要使用数据,

。另一种方法是在完成内存流之前不要调用 Tiff.Close。不过,这种方法有一些缺点。

The problem is:

Tiff object closes and disposes stream after call to Close method.

So, you probably should change

MemoryStream newStream = (MemoryStream)tiff.Clientdata();

to

MemoryStream newStream = new MemoryStream(ms.ToArray());

if you need to use data later.

Another approach is to NOT call Tiff.Close until you are done with the memory stream. this approach has some drawbacks, though.

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