C# 当前在其他地方使用的对象(System.Drawing)
这是我用来生成PDF的代码C# 当前在其他地方使用的对象(System.Drawing),c#,asp.net,system.drawing,C#,Asp.net,System.drawing,这是我用来生成PDF的代码 public string ScreenshotFullLength(string Url) { UrlScreenshot Shot = new UrlScreenshot(Url, 975, 100); int maxHeight = 1250; // If you do skip the crop function you 'll get the // full lenght of the page. We'll scale i
public string ScreenshotFullLength(string Url) {
UrlScreenshot Shot = new UrlScreenshot(Url, 975, 100);
int maxHeight = 1250;
// If you do skip the crop function you 'll get the
// full lenght of the page. We'll scale it to match
// a 400 pixel width
//int newHeight = (Shot.Bitmap.Width / 400) * Shot.Bitmap.Height;
//Shot.Resize(400, newHeight);
string Filename = LazyAssFilename();
string path = Server.MapPath("~") + "/tmp/" + Filename;
Shot.Bitmap.Save(path, ImageFormat.Png);
string pdfURL = "";
Document document = new Document();
try {
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
pdfURL = Server.MapPath("~") + "\\tmp\\Attest_" + EOFName + ".pdf";
PdfWriter.GetInstance(document, new FileStream(pdfURL, FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we add content
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(path);
if (jpg.Height > maxHeight) {
//we moeten er meer dan 1 maken en croppen
int loops = (int)(jpg.Height / maxHeight);
int rest = (int)(jpg.Height % maxHeight);
int i;
for (i = 0; i < loops; i++) {
Bitmap bmpImage = new Bitmap(path);
Bitmap bmpCrop = bmpImage.Clone(new System.Drawing.Rectangle(0, i * maxHeight, 975, maxHeight),
bmpImage.PixelFormat);
iTextSharp.text.Image crpd = iTextSharp.text.Image.GetInstance(bmpCrop, ImageFormat.Png);
crpd.Alignment = iTextSharp.text.Image.MIDDLE_ALIGN;
crpd.ScalePercent(60);
document.Add(crpd);
}
//the rest
Bitmap bmpImage2 = new Bitmap(path);
Bitmap bmpCrop2 = bmpImage2.Clone(new System.Drawing.Rectangle(0, i * maxHeight, 975, rest),
bmpImage2.PixelFormat);
iTextSharp.text.Image crpdRest = iTextSharp.text.Image.GetInstance(bmpCrop2, ImageFormat.Png);
crpdRest.Alignment = iTextSharp.text.Image.MIDDLE_ALIGN;
crpdRest.ScalePercent(60);
document.Add(crpdRest);
} else {
jpg.Alignment = iTextSharp.text.Image.MIDDLE_ALIGN;
jpg.ScalePercent(60);
document.Add(jpg);
}
} catch (DocumentException de) {
Console.Error.WriteLine(de.Message);
} catch (IOException ioe) {
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
try {
//screenshots deleten
File.Delete(path);
} catch { }
return pdfURL;
}
公共字符串截图全长(字符串Url){
UrlScreenshot=新的UrlScreenshot(Url,975100);
int maxHeight=1250;
//如果你跳过裁剪功能,你会得到
//页面的完整长度。我们将缩放它以匹配
//400像素宽
//int newHeight=(Shot.Bitmap.Width/400)*Shot.Bitmap.Height;
//快照。调整大小(400,新高度);
字符串Filename=LazyAssFilename();
字符串路径=Server.MapPath(“~”+”/tmp/“+文件名;
Shot.Bitmap.Save(路径,ImageFormat.Png);
字符串pdfURL=“”;
文档=新文档();
试一试{
//步骤2:
//我们创建一个侦听文档的编写器
//并将PDF流定向到文件
pdfURL=Server.MapPath(“~”+”\\tmp\\authentic_“+EOFName+”.pdf”;
GetInstance(文档,新文件流(pdfURL,FileMode.Create));
//步骤3:我们打开文档
document.Open();
//步骤4:我们添加内容
iTextSharp.text.Image jpg=iTextSharp.text.Image.GetInstance(路径);
如果(jpg.Height>maxHeight){
//我们将继续前进,并将其缩短
int循环=(int)(jpg.Height/maxHeight);
int rest=(int)(jpg.Height%maxHeight);
int i;
对于(i=0;i
这在网站上运行,以制作网页的PDF。
但是,当多人从网站访问此代码以生成他们的PDF时,我会得到错误:对象当前正在其他地方使用。
STACKTRACE:at System.Drawing.Image.Save(字符串文件名、ImageCodeInfo编码器、EncoderParameters encoderParams)at
我该如何解决这个问题?在Shot.Bitmap.Save(路径,ImageFormat.Png)处抛出错误;
刚刚发现了类似的情况。其中一个答案表明GDI+不是线程安全的,在这种情况下,您需要在Save()周围加上一个锁,也许还需要一些其他方法
只是为了确认一下,阅读类的信息,尽管这是指单个实例还是同一类的不同实例,这是不明确的
此类型的任何公共静态(在Visual Basic中共享)成员都是
线程安全。不保证任何实例成员都是线程
安全的
我遇到了同样的问题,我有一个工作线程将GUI输出到它的图形界面,但它只是在某些时候抛出这个异常。 用Invoke包装它修复了它
_parent.Invoke( new Action(() => eg.Graphics.DrawImage( _icon, rc )));
哎呀!千万不要在没有身体的情况下尝试{…}捕捉{},所有的陌生感都会在那里被捕捉到。这是什么类型的异常?可能最好只发布完整的异常。ToString()Ian:MESSAGE:Object当前正在其他地方使用。问题是我很难在本地重现这个错误。