Quantcast
Viewing all articles
Browse latest Browse all 3693

New Post: Image corruption issue

I'm experiencing a similar issue, but when converting a multi-page PDF to a series of single-page TIFFs. Some of the resulting TIFF files are blank and are noticeably smaller than the other files. Like @arthg, I'm seeing this problem when selecting CompressionMethod.Group4 or CompressionMethod.Fax. If I select CompressionMethod.LZW, all resulting TIFF files are valid, but are MUCH larger.

FWIW, If I convert the input PDF manually using the IM convert utility, the output files are all OK. Here is a sample of how I converted the first page.

convert -density 300 -compress group4 "sample6.pdf[0]" sample6p1.tiff
private static void ConvertImageToTiff()
{
  // Log all events
  //MagickNET.SetLogEvents(LogEvents.All | LogEvents.Trace);
  // Set the log handler (all threads use the same handler)
  //MagickNET.Log += DetailedDebugInformationSamples.MagickNET_Log;

  string sampleDocsDirectory = @"E:\projects\ImageProcessing\sampledocs\";
  string sampleFile = "sample6.pdf";

  try
  {
    MagickReadSettings settings = new MagickReadSettings();
    settings.Density = new PointD(300, 300);

    using (MagickImageCollection images = new MagickImageCollection())
    {
      // Add all the pages of the source file to the collection
      images.Read(Path.Combine(sampleDocsDirectory, sampleFile), settings);

      //Show page count
      Console.WriteLine("page count for {0} {1}", sampleFile, images.Count);

      //Base file name
      string baseFileName = Path.GetFileNameWithoutExtension(sampleFile);
      string fmtStr = GetPageNumberFormat(images.Count);
      
      int page = 1;
      foreach (MagickImage image in images)
      {
        switch (image.Format)
        {
          case MagickFormat.Jpeg:
          case MagickFormat.Jpg:
            image.CompressionMethod = CompressionMethod.LZW;
            break;
          case MagickFormat.Pdf:
            image.CompressionMethod = CompressionMethod.Group4;
            //image.CompressionMethod = CompressionMethod.LZW;
            break;
          case MagickFormat.Tif:
          case MagickFormat.Tiff:
          case MagickFormat.Tiff64:
            break;
          default:
            image.CompressionMethod = CompressionMethod.LZW;
            break;
        }
        
        Console.WriteLine("saving file: {0}", baseFileName + ".p" + page.ToString(fmtStr) + ".tif");
        image.Write(sampleDocsDirectory + baseFileName + ".p" + page.ToString(fmtStr) + ".tif");
        page++;
      }
    }

  }
  catch (Exception ex)
  {
    Console.WriteLine("ReadPdf error {0}", ex.Message);
  }
}
I will attach sample input and output image files to workitem 1373.

Thank you for your help!

Viewing all articles
Browse latest Browse all 3693

Trending Articles