如何诊断 freeimage loadfromstream 错误

本文关键字:freeimage loadfromstream 错误 诊断 何诊断 | 更新日期: 2023-09-27 18:33:12

我正在使用C# FreeImage包装器。我正在尝试打开一个包含图像的PDF文件,并将这些图像"提取"到Windows位图对象中。我遵循互联网上文章中描述的准则,大致遵循以下模式:

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 函数,是否有人对如何解决此问题有建议?谢谢

如何诊断 freeimage loadfromstream 错误

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

也就是说,使用以下方法将函数添加到代码中:

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

以下代码显示了从 Save 函数引发的异常,而不是返回 false(具有以下控制台输出):

测试 1...成功
测试 2...
未处理的异常:System.Exception:只能将 24 位高色或 8 位灰度/调色板位图另存为 JPEG
at ConsoleApplication1.Program.OutputMessage(FREE_IMAGE_FORMAT format, String message) ...''ConsoleApplication1''Program.cs:line 35
at FreeImageAPI.FreeImage.Save(FREE_IMAGE_FORMAT fif, FIBITMAP dib, String filename, FREE_IMAGE_SAVE_FLAGS flags)
at ConsoleApplication1.Program.Main(String[] args) in ...''ConsoleApplication1''Program.cs:line 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);
   }
}