2011-10-06 45 views
0

這是我運行生成PDF對象別處(System.Drawing中)

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; 
} 

這將運行一個網站上,使網頁的PDF的代碼。 然而,當多個用戶從網站訪問該代碼,生成其PDF格式的,我得到的錯誤:Object is currently in use elsewhere.

堆棧跟蹤:在System.Drawing.Image.Save(字符串文件名,ImageCodecInfo編碼器,EncoderParameters encoderParams)在.. 。

我該如何解決這個問題?錯誤發生在Shot.Bitmap.Save(path, ImageFormat.Png);

+0

哎呀!千萬不要'嘗試{...}沒有身體的抓住{},各種奇怪的東西都會被抓到。 –

+1

它是什麼類型的例外?可能最好只發布完整的異常.ToString() – Ian

+0

Ian:MESSAGE:Object目前正在其他地方使用。問題在於我在本地重現此錯誤非常困難。 – Stefanvds

回答

0

剛剛發現了這個相似的question。其中一個答案表明GDI +不是線程安全的,在這種情況下,您需要鎖定Save()以及其他一些方法。

只是爲了確認,閱讀Image類的信息,儘管它對於這是指單個實例還是同一類的不同實例是不明確的。

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

0

我遇到了同樣的問題,我有一個工作線程抽出GUI到它的圖形界面,但它只在某些時候拋出這個異常。 用Invoke包裝它固定它。

_parent.Invoke(new Action(() => eg.Graphics.DrawImage(_icon, rc)));