首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >释放另一个进程正在使用的文件

释放另一个进程正在使用的文件
EN

Stack Overflow用户
提问于 2014-03-05 14:29:40
回答 1查看 646关注 0票数 0

我正在实现“复制地图剪辑”作为一个图像。

我所做的就是创建一个图像,将其保存到特定的目录中,并将其复制到剪贴板上。

但是在我这样做之前,我删除了目录中存在的所有文件,但现在我不能这样做,因为剪贴板正在使用该图像。

这是我的密码。

代码语言:javascript
复制
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 {}
        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-05 14:41:08

您需要在将图像插入剪贴板后释放它:

代码语言:javascript
复制
using (System.Drawing.Image objImage = System.Drawing.Bitmap.FromFile(fileName))
{
    System.Windows.Forms.Clipboard.SetImage(objImage);
}

否则,它将保持打开状态,直到垃圾收集器调用objImage的终结器。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22200617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档