2

I need your help adding an image to a PDF.

I'm using:

string imgPath2 = localPath + "\\TempChartImages\\" + LegendPath;
img2.Save(imgPath2);
ith.WriteImage(imgPath2, 80);

But this code gives me the error:

Use of unassigned local variable img2

How can I solve this error?

2
  • I'm not sure what your code does (as its not very clear) but are you trying to add an image to a PDF? If so, use iTextSharp. Commented Jun 18, 2009 at 13:54
  • Can you post the code that defines img2? You will need a library to do the insert in the PDF as there is nothing backed into the .NET framework to manipulate PDFs. Commented Jun 18, 2009 at 22:49

6 Answers 6

2

Here is the iTextSharp tutorial on images. Without seeing more of your code, it's tough to judge what piece of code from this you'll need.

Sign up to request clarification or add additional context in comments.

Comments

2

When you declare a variable, in your case img2, without assigning a value it is pointing to absolutely nothing. Make sure you initialize img2 to something before using it.

I think what you want your img2.Save line to be changed to:

Image img2 = Image.FromFile(yourInitialImageHere);  // You could be reading from memory as well.
img2.Save(imgPath2);

I could be way off though as your snippet of code is pretty vague.

Comments

2

it's a hunch, but if you're assigning the value of img2 inside a Try-Catch block you might be hitting an exception that prevents the assignment from taking place. For example:

var img2;
try
{
    var x = 5 / 0; // Generate a DivideByZero exception
    img2 = GetImage(); // <-- the above exception will prevent this code from executing
}
catch
{
}
img2.Save(imgPath2); <-- img2 wasn't assigned, so another exception will occur

Comments

1

You'll need some third-party tool for this.

1 Comment

It would be worth editing your question to include that information - probably even in the question header.
1

I believe you have to instantiate first the Image.

Image img2 = new Image();

it solved my problems. Hope it will solve yours.

Comments

1

You have to create a getinstance of the image.

iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance("path of the image");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.