2017-04-23 735 views
1

打開我使用到PdfiumViewer PDF轉換爲圖像C#關閉PDF與PdfiumViewer

var document = PdfiumViewer.PdfDocument.Load(pdfoutputFilePath + TafCode + ".pdf"); 
var image = document.Render(0, 842, 595, true); 
image.Save(imageoutputFilePath + TafCode + ".png", ImageFormat.Png); 

當我做我刪除PDF

錯誤打開PDF中號!

pdf如何關閉?

回答

1

將保存到圖像代碼中用於確保調用「Dispose」並解除資源鎖定。

using (var document = PdfiumViewer.PdfDocument.Load(pdfoutputFilePath + TafCode + ".pdf")) 
{ 
    var image = document.Render(0, 842, 595, true); 
    image.Save(imageoutputFilePath + TafCode + ".png", ImageFormat.Png); 
} 

你可以告訴大家通過看PdfDocument類的代碼定義「處置」的方法來釋放相應的文件。

public void Dispose() 
    { 
     Dispose(true); 
     GC.SuppressFinalize(this); 
    } 

    /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary> 
    /// <param name="disposing">Whether this method is called from Dispose.</param> 
    protected void Dispose(bool disposing) 
    { 
     if (!_disposed && disposing) 
     { 
      if (_file != null) 
      { 
       _file.Dispose(); 
       _file = null; 
      } 

      _disposed = true; 
     } 
    }