如何诊断freeimage loadfromstream错误

时间:2014-12-04 16:39:00

标签: c# pdf freeimage

我正在使用C#FreeImage包装器。 我正在尝试打开包含图像的PDF文件,并提取"提取"那些图像进入windows Bitmap对象。我遵循互联网上的文章中描述的指导原则,松散地遵循以下模式:

byte[] bytes = GetPdfFile();

iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(bytes);

int pageNumber = 0;
for (int i = 0; i <= reader.XrefSize - 1; i++)
{
    pdfObj = reader.GetPdfObject(i);
    if ((pdfObj != null) && pdfObj.IsStream())
    {
        pdfStream = (iTextSharp.text.pdf.PdfStream)pdfObj;
        iTextSharp.text.pdf.PdfObject subtype = pdfStream.Get(iTextSharp.text.pdf.PdfName.SUBTYPE);
        if ((subtype != null) && subtype.ToString().Equals(iTextSharp.text.pdf.PdfName.IMAGE.ToString()))
        {
            pageNumber++;
            byte[] imgBytes = iTextSharp.text.pdf.PdfReader.GetStreamBytesRaw((iTextSharp.text.pdf.PRStream)pdfStream);
            if ((imgBytes != null))
            {
                // in my case images are in TIF Group 4 format
                using (MemoryStream ms = new MemoryStream(imgBytes))
                {
                    FreeImageAPI.FIBITMAP bmp = FreeImageAPI.FreeImage.LoadFromStream(ms);

                    // in my case bmp with IsNull = true is returned
                    if (!bmp.IsNull)
                    {
                        using (MemoryStream msOut = new MemoryStream())
                        {
                            FreeImageAPI.FreeImage.SaveToStream(bmp, msOut, ... ); // etc.
                        }
                    }
                }
            }
        }
    }
}

有没有人建议如何解决这个问题,因为没有返回任何异常 - 某种GetLastError FreeImage函数? 谢谢

1 个答案:

答案 0 :(得分:0)

虽然似乎没有将SetOutputMessage函数添加到.NET包装器库中,但可以直接从FreeImage库中调用它。

即,使用以下命令将函数添加到代码中:

  [DllImport("FreeImage", EntryPoint = "FreeImage_SetOutputMessage")]
  internal static extern void SetOutputMessage(OutputMessageFunction outputMessageFunction);

以下代码显示从Save函数抛出异常,而不是返回false(使用以下控制台输出):

  

测试1 ...成功
  测试2 ...
  未处理的异常:System.Exception:只能将24位高色或8位灰度/调色板位图保存为JPEG
     在ConsoleApplication1.Program.OutputMessage(FREE_IMAGE_FORMAT格式,字符串消息)... \ ConsoleApplication1 \ Program.cs:第35行
     在FreeImageAPI.FreeImage.Save(FREE_IMAGE_FORMAT fif,FIBITMAP dib,String filename,FREE_IMAGE_SAVE_FLAGS标志)
     在ConsoleApplication1.Program.Main(String [] args)中... \ ConsoleApplication1 \ Program.cs:第28行

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using FreeImageAPI;

namespace ConsoleApplication1
{
   class Program
   {
      static void Main(string[] args)
      {  FIBITMAP bitmap;
         bool    success;

         SetOutputMessage(OutputMessage);

         Console.Write("Test 1... ");
         bitmap = FreeImage.CreateFromBitmap(new Bitmap(320, 240, PixelFormat.Format24bppRgb));
         success = FreeImage.Save(FREE_IMAGE_FORMAT.FIF_JPEG, bitmap, "output.jpg",   FREE_IMAGE_SAVE_FLAGS.JPEG_QUALITYNORMAL);
         FreeImage.Unload(bitmap);
         Console.WriteLine("Success");

         Console.Write("Test 2... ");
         bitmap = FreeImage.CreateFromBitmap(new Bitmap(320, 240));
         success = FreeImage.Save(FREE_IMAGE_FORMAT.FIF_JPEG, bitmap, "output.jpg",   FREE_IMAGE_SAVE_FLAGS.JPEG_QUALITYNORMAL);
         FreeImage.Unload(bitmap);
         Console.WriteLine("Success");

      }

      static void OutputMessage(FREE_IMAGE_FORMAT format, string message)
      {  throw new Exception(message);
      }

      [DllImport("FreeImage", EntryPoint = "FreeImage_SetOutputMessage")]
      internal static extern void SetOutputMessage(OutputMessageFunction outputMessageFunction);

   }
}