Object currently in use elsewhere (System.Drawing)
This is the code which I run to generate a 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 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;
}
This runs on a website, to make a PDF of a webpage.
However when multiple people access this code from the website, to generate their PDF's, I get the error: Object is currently in use elsewhere.
STACKTRACE: at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) at ...
How would I fix this? The error gets thrown at Shot.Bitmap.Save(path, ImageFormat.Png);
I encountered the same problem, I have a worker thread pumping out GUI to its graphical interface, but it only throws this exception some of the time. Wrapping it with an Invoke fixed it.
_parent.Invoke( new Action(() => eg.Graphics.DrawImage( _icon, rc )));
Just spotted this similar question. One of the answers suggests that GDI+ isn't threadsafe, in which case you'll need a lock around your Save() and maybe a few other methods.
Just to confirm, reading the information for the Image class, although it is ambiguous as to whether this means a single instance, or different instances of the same class.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
精彩评论