I need your help adding an image to a PDF.
我需要您的帮助将图像添加到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
使用未分配的局部变量img2
How can I solve this error?
我该如何解决这个错误?
6 个解决方案
#1
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.
这是关于图像的iTextSharp教程。如果没有看到更多的代码,很难判断出你需要的代码片段。
#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.
当你声明一个变量时,在你的情况下img2,没有指定一个值,它指向绝对没有。确保在使用之前将img2初始化为某些内容。
I think what you want your img2.Save
line to be changed to:
我想你想要将你的img2.Save行更改为:
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.
我可能会离开,因为你的代码片段非常含糊。
#3
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:
这是一种预感,但是如果你在Try-Catch块中分配img2的值,你可能会遇到阻止分配发生的异常。例如:
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
#4
You'll need some third-party tool for this.
你需要一些第三方工具。
#5
I believe you have to instantiate first the Image.
我相信你必须首先实例化Image。
Image img2 = new Image();
it solved my problems. Hope it will solve yours.
它解决了我的问题。希望它能解决你的问题。
#6
You have to create a getinstance of the image.
您必须创建图像的getinstance。
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance("path of the image");
#1
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.
这是关于图像的iTextSharp教程。如果没有看到更多的代码,很难判断出你需要的代码片段。
#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.
当你声明一个变量时,在你的情况下img2,没有指定一个值,它指向绝对没有。确保在使用之前将img2初始化为某些内容。
I think what you want your img2.Save
line to be changed to:
我想你想要将你的img2.Save行更改为:
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.
我可能会离开,因为你的代码片段非常含糊。
#3
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:
这是一种预感,但是如果你在Try-Catch块中分配img2的值,你可能会遇到阻止分配发生的异常。例如:
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
#4
You'll need some third-party tool for this.
你需要一些第三方工具。
#5
I believe you have to instantiate first the Image.
我相信你必须首先实例化Image。
Image img2 = new Image();
it solved my problems. Hope it will solve yours.
它解决了我的问题。希望它能解决你的问题。
#6
You have to create a getinstance of the image.
您必须创建图像的getinstance。
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance("path of the image");