我想使用LibTiff.Net从流中读取TIFF图像。当我使用"r"
模式打开图片时,该库可以正常工作,但当我使用"w"
模式时,它会返回null
。
这是我使用的方法。
Tiff image = Tiff.ClientOpen("in-memory", "w", memStream, new TiffStream());
我使用GhostScript.Net
将PDF转换为TIFF图像,然后使用LibTiff.Net
编写Tiff标签。这里有更多代码:
MemoryStream memStream = new MemoryStream();
using (GhostscriptRasterizer rasterizer = new GhostscriptRasterizer())
{
rasterizer.Open(datastream, _lastInstalledVersion, false);
System.Drawing.Image images = rasterizer.GetPage(desired_x_dpi, desired_y_dpi, 1);
Bitmap bmp = new Bitmap(images);
bmp.SetResolution(200,200);
images = (System.Drawing.Image)bmp;
ImageCodecInfo tiffEncoder = ImageCodecInfo.GetImageEncoders().SingleOrDefault(s => s.MimeType == "image/tiff");
EncoderParameters myEncoderParameters = new EncoderParameters(3);
EncoderParameter myEncoderParameter = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.MultiFrame);
myEncoderParameters.Param[0] = myEncoderParameter;
myEncoderParameters.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionCCITT4);
myEncoderParameters.Param[2] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 200L);
images.Save(memStream, tiffEncoder, myEncoderParameters);
memStream.Position = 0;
TiffStream tifstr = new TiffStream();
Tiff image = Tiff.ClientOpen("in-memory", "w", memStream, tifstr);
image.SetField(TiffTag.SUBFILETYPE, FileType.PAGE);
image.SetField(TiffTag.IMAGEWIDTH, 1704);
image.SetField(TiffTag.IMAGELENGTH, 2200);
image.SetField(TiffTag.BITSPERSAMPLE, 1);
image.SetField(TiffTag.COMPRESSION, Compression.CCITT_T4);
image.SetField(TiffTag.PHOTOMETRIC, Photometric.MINISWHITE);
image.SetField(TiffTag.FILLORDER, FillOrder.LSB2MSB);
image.SetField(TiffTag.ORIENTATION, Orientation.TOPLEFT);
image.SetField(TiffTag.SAMPLESPERPIXEL, 1);
image.SetField(TiffTag.ROWSPERSTRIP, 2200);
image.SetField(TiffTag.MINSAMPLEVALUE, 0);
image.SetField(TiffTag.MAXSAMPLEVALUE, 1);
image.SetField(TiffTag.XRESOLUTION, 200);
image.SetField(TiffTag.YRESOLUTION, 200);
image.SetField(TiffTag.PLANARCONFIG, PlanarConfig.CONTIG);
image.SetField(TiffTag.RESOLUTIONUNIT, ResUnit.INCH);
image.WriteDirectory();
memStream = (MemoryStream)image.Clientdata();
}
如果我使用"w"
模式,看起来流位置不能设置为0。
现在工作正常。
但写完标签后,无法打开TIFF图像,在Windows Photo Viewer中,它说
file
appeared to damaged , corrupted or it's too large
使用LibTiff.NET
时,我有什么遗漏的吗?
当我使用"r"
模式时,它没问题。但"w"
模式会破坏图像文件。