【发布时间】:2015-03-21 18:46:31
【问题描述】:
以下代码用于加载嵌入的 GIF 图像资源,以插入 PDF。不幸的是,iTextSharp.text.Image.GetInstance() 生成了异常消息:
对象引用未设置为对象的实例。
我相信这意味着某些不应该是 null 的东西。但是对我来说,使用 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)。感谢@QuietSeditionist 的提示。
标签: c# itextsharp gif embedded-resource