views:

1184

answers:

5

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 give me error : "Use of unassigned local varible img2"

How can I solve this error?

+1  A: 

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

Jeffrey
Yes i have i used itextSharp....
Phsika
It would be worth editing your question to include that information - probably even in the question header.
Jeffrey
+2  A: 

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.

Jeffrey
+1  A: 

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.

Kelsey
+1  A: 

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
STW
A: 

Hi Check here link text

I am seeing a article named "How to export a image to pdf using itextsharp dll"

peter