user2875912
user2875912

Reputation: 109

Release a file being used by another process

I'm implementing "copy map to clip" as an image.

what I do is I create an image an save it to certain directory and copy it to clipboard.

but before I do that I delete every file that exist in the directory, but now I cant since, the image is being used by clipboard.

here's my code.

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

Upvotes: 0

Views: 764

Answers (1)

Matthew Watson
Matthew Watson

Reputation: 109567

You need to dispose the image after inserting it into the clipboard:

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

Otherwise it will remain open until the garbage collector calls the finalizer for objImage.

Upvotes: 1

Related Questions