2014-03-05 27 views
0

我正在實現「將映射覆制到剪輯」作爲圖像。釋放另一個進程正在使用的文件

我做的是我創建一個圖像並將其保存到某個目錄並將其複製到剪貼板。

但是在我這樣做之前,我刪除了目錄中存在的每個文件,但是現在我不能這樣做,圖像被剪貼板使用了。

這是我的代碼。

public override void OnClick() 
     { 
      //base.OnClick(); 
      DeleteOldCopiedJPG(); 
      System.Windows.Forms.Clipboard.Clear(); 
      string fileName = System.Windows.Forms.Application.ExecutablePath.Substring(0, System.Windows.Forms.Application.ExecutablePath.LastIndexOf("\\") + 1) + Guid.NewGuid() + ".jpg"; 
      //System.Windows.Forms.Cursor.Current = Cursors.Wait; 
      //if (System.IO.File.Exists(fileName)) 
      // System.IO.File.Delete(fileName); 
      IExport objExport = (IExport)new ExportJPEG(); 
      objExport.ExportFileName = fileName; 
#if Debug || Release 
      ESRI.ArcGIS.Display.tagRECT objExportRECT = default(ESRI.ArcGIS.Display.tagRECT); 
#else 
      tagRECT objExportRECT = default(tagRECT); 
#endif 
      var _with1 = objExportRECT; 
      _with1.left = mapControl.ActiveView.ExportFrame.left; 
      _with1.top = mapControl.ActiveView.ExportFrame.top; 
      _with1.right = mapControl.ActiveView.ExportFrame.right; 
      _with1.bottom = mapControl.ActiveView.ExportFrame.bottom; 

      IEnvelope envelope = new EnvelopeClass(); 
      envelope.PutCoords(mapControl.ActiveView.ExportFrame.left, mapControl.ActiveView.ExportFrame.top, 
       mapControl.ActiveView.ExportFrame.right, mapControl.ActiveView.ExportFrame.bottom); 
      objExport.PixelBounds = envelope; 

      System.Int32 intHDC = objExport.StartExporting(); 
      mapControl.ActiveView.Output(intHDC, Convert.ToInt16(objExport.Resolution), objExportRECT, null, null); 
      objExport.FinishExporting(); 
      objExport.Cleanup(); 

      System.Drawing.Image objImage = System.Drawing.Bitmap.FromFile(fileName); 
      System.Windows.Forms.Clipboard.SetImage(objImage); 

      //RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(200, 200, 96, 96, PixelFormats.Pbgra32); 
      //renderTargetBitmap.Render((Visual)mapControl.ActiveView.ScreenDisplay); 
      //Clipboard.SetImage(renderTargetBitmap); 
     } 

private void DeleteOldCopiedJPG(string Path) 
     { 
      string[] filePaths = Directory.GetFiles(System.Windows.Forms.Application.ExecutablePath.Substring(0, System.Windows.Forms.Application.ExecutablePath.LastIndexOf("\\") + 1)); 
      foreach (string filepath in filePaths) 
       if (filepath.Substring(filepath.Length - 4) == ".jpg") 
        try{ File.Delete(filepath); } catch {} 
     } 

回答

1

您需要將其插入到剪貼板後處理圖像:

using (System.Drawing.Image objImage = System.Drawing.Bitmap.FromFile(fileName)) 
{ 
    System.Windows.Forms.Clipboard.SetImage(objImage); 
} 

否則,直到垃圾回收器調用終結了objImage將繼續開放。

+0

非常感謝你@Matthew Watson它工作**完美**。 – user2875912

相關問題