使用嵌入式图像 (gif) 资源时出错

Error using embedded image (gif) resource

以下代码用于加载嵌入的 GIF 图像资源,以插入到 PDF 中。不幸的是,iTextSharp.text.Image.GetInstance() 生成异常消息:

Object reference not set to an instance of an object.

我认为这意味着某些不应该为空的东西。但是使用 Visual C# Express 调试器单步执行代码并没有向我揭示它可能是什么。

我想知道更有经验的 C#/iTextSharp 黑客是否能够发现我哪里出错了?

using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace giftest
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                System.IO.Stream s =
                    System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("giftest.clear.gif");
                iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(s);
                Console.WriteLine("Press any key to continue...");
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Some sort of error occured: " + ex.Message);
                Console.WriteLine();
                Console.WriteLine("Press any key to continue...");
                Console.ReadKey();
            }
        }
    }
}
/* clear.gif
 * data:image/gif;base64,R0lGODlhAQABAHAAACH5BAUAAAAALAAAAAABAAEAAAICRAEAOw==
 */

首先尝试从流中获取图像,然后将其发送到 GetInstance() 调用。正如您所建议的:iTextSharp.text.Image.GetInstance(System.Drawing.Image.FromStream(s), System.Drawing.Imaging.ImageFormat.Gif)