简体   繁体   English

当前在其他地方使用的对象(System.Drawing)

[英]Object currently in use elsewhere (System.Drawing)

This is the code which I run to generate a PDF 这是我运行以生成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. 它在网站上运行,以制作网页PDF。 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. 但是,当多个人从网站访问此代码以生成其PDF时,我得到了错误: Object is currently in use elsewhere.

STACKTRACE: at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) at ... STACKTRACE:位于System.Drawing.Image.Save(字符串文件名,ImageCodecInfo编码器,EncoderParameters encoderParams)位于...

How would I fix this? 我该如何解决? The error gets thrown at Shot.Bitmap.Save(path, ImageFormat.Png); 该错误被抛出到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. 我遇到了同样的问题,我有一个工作线程将GUI抽出到其图形界面,但有时它只会引发此异常。 Wrapping it with an Invoke fixed it. 用Invoke包装将其固定。

_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. 答案之一表明GDI +不是线程安全的,在这种情况下,您将需要对Save()以及其他一些方法进行锁定。

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. 只是为了确认,阅读有关Image类的信息,尽管对于这意味着单个实例还是同一类的不同实例尚不明确。

Any public static (Shared in Visual Basic) members of this type are thread safe. 此类型的任何公共static(在Visual Basic中为Shared)成员都是线程安全的。 Any instance members are not guaranteed to be thread safe. 不保证任何实例成员都是线程安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM