【问题标题】:Need to delete a file, in use by my own app需要删除一个文件,我自己的应用程序正在使用
【发布时间】:2010-02-19 18:32:03
【问题描述】:

我正在摆弄一个桌面小工具(时钟)。它下面有一个反射效果,需要透明,我使用 CopyFromScreen 来获取背景,然后将表单背景设置为此。

像这样(“停靠/取消停靠”按钮的一部分):

        Rectangle bounds = this.Bounds;

        using (Bitmap ss = new Bitmap(bounds.Width, bounds.Height))
        using (Graphics g = Graphics.FromImage(ss))
        {
            this.Opacity = 0;

            g.CopyFromScreen(this.Location, Point.Empty, bounds.Size);
            ss.Save(@"C:\clock1s\bg", ImageFormat.Png);

            this.Opacity = 100;
            this.BackgroundImage = new Bitmap(@"C:\clock1s\bg");

            g.Dispose();
        }

现在,每当我想再次使用它(例如移动时钟)时,我都无法删除该文件或重新保存它,因为它当前正在使用中。我已经尝试将表单 BG 设置为其他内容,但这不起作用。

我该怎么做?

编辑:已排序,谢谢 (Lance McNearney)。

现在,如果我不得不将它保存到文件中呢?

另外,额外的问题:

the goddamn batwatch http://upload.snelhest.org/images/100219batwatch.jpg

选择和图标最终位于表单下方是一个小麻烦,如果可能的话,我希望它们最终位于顶部或下方(保持平滑的抗锯齿)。不过,我假设这是我无法解决的问题。

【问题讨论】:

    标签: c# winforms image file desktop


    【解决方案1】:

    您真的需要将图像保存到文件系统吗?你不能只为你的应用程序将它存储在内存中(这样你就不会再遇到问题了)?

    MemoryStream 应该可以直接替换代码中的文件路径(尽管我显然无法编译来测试它):

    Rectangle bounds = this.Bounds;
    
    using (Bitmap ss = new Bitmap(bounds.Width, bounds.Height))
    using (Graphics g = Graphics.FromImage(ss))
    {
        this.Opacity = 0;
        g.CopyFromScreen(this.Location, Point.Empty, bounds.Size);
    
        using(MemoryStream ms = new MemoryStream()) {
            ss.Save(ms, ImageFormat.Png);
    
            this.Opacity = 100;
            this.BackgroundImage = new Bitmap(ms);
        }
    
        g.Dispose();
    }
    

    【讨论】:

    • 呃。谢谢。稍后标记您的答案,允许人们对第二部分发表评论
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-02
    • 2019-10-28
    • 2013-05-13
    • 2012-10-27
    相关资源
    最近更新 更多