如何使用 Bit Miracle LibTiff.Net 将图像写入 MemoryStream
我收到了使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是:
Tiff
对象在调用Close
方法后关闭并处置流。您可能应该更改
为
因此,如果稍后需要使用数据,
。另一种方法是在完成内存流之前不要调用 Tiff.Close。不过,这种方法有一些缺点。
The problem is:
Tiff
object closes and disposes stream after call toClose
method.So, you probably should change
to
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.