1

I want to use LibTiff.Net to read TIFF images from stream. The library works when I open images with under "r" mode, but when I use "w" mode, it returns null.

Here is the method I used.

Tiff image = Tiff.ClientOpen("in-memory", "w", memStream, new TiffStream());

I use GhostScript.Net convert a PDF to TIFF image, then use LibTiff.Net write the Tiff tags.Here is more code:

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();

            }

It looks like stream position can not be set to 0 if I use "w" mode. It works fine now.

But after write the tags, the TIFF Image can not be opened, in Windows Photo Viewer, it said the

file 
appeared to damaged , corrupted or it's too large

.

Is there anything I missed when I use LibTiff.NET ?

When I use "r" mode, it's fine. But "w" mode will break the image file.

  • Do you get any errors / warning in console? The library should output something there. Please also make sure that you memory stream is expandable and writeable. – Bobrovsky Sep 18 '14 at 05:13
  • There is no warning/errors, and the stream is writeable @Bobrovsky – user3780941 Sep 18 '14 at 05:42
  • Thank you, please post more code (stream creation, error checking). The line you posted should work just fine. – Bobrovsky Sep 18 '14 at 06:45
  • I just posted more code,thank you @Bobrovsky – user3780941 Sep 18 '14 at 13:24
  • I re-checked the library and it works as expected when used with `w` mode. I don't know what issue you are observing but I want to note that you code is probably meaningless. – Bobrovsky Sep 18 '14 at 16:09
  • what do you mean meaningless? @Bobrovsky – user3780941 Sep 18 '14 at 16:14
  • Could you try to replace this: Bitmap bmp = new Bitmap(images); with Bitmap bmp = new Bitmap(images.Clone() as System.Drawind.Image); I remember someone had some problem with corrupted image returned from Ghostscript.NET. – HABJAN Sep 25 '14 at 10:19

0 Answers0