0

The following code is meant to load an embedded GIF image resource, to be inserted into a PDF. Unfortunately, iTextSharp.text.Image.GetInstance() generates an exception message:

Object reference not set to an instance of an object.

I believe that means something is null that shouldn't be. But stepping through the code using the Visual C# Express debugger hasn't revealed what it might be, to me.

I was wondering if a more experienced C#/iTextSharp hacker might be able to spot where I'm going wrong?

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==
 */
Thomas Weller
  • 43,638
  • 16
  • 101
  • 185
minipc
  • 73
  • 5
  • Try getting an Image from your stream first, then send that to the GetInstance() call. – QuietSeditionist Jan 22 '15 at 20:20
  • So, it would seem the solution is to `iTextSharp.text.Image.GetInstance(System.Drawing.Image.FromStream(s), System.Drawing.Imaging.ImageFormat.Gif)`. Thanks to @QuietSeditionist for the hint. – minipc Jan 23 '15 at 00:54
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Thomas Weller Mar 28 '15 at 21:19

1 Answers1

1

Try getting an Image from your stream first, then send that to the GetInstance() call. As you suggested: iTextSharp.text.Image.GetInstance(System.Drawing.Image.FromStream(s), System.Drawing.Imaging.ImageFormat.Gif)

QuietSeditionist
  • 624
  • 3
  • 8
  • 18