需要删除我自己的应用程序使用的文件

pas*_*ets 2 c# desktop image file winforms

我正在摆弄一个桌面小工具(一个时钟).它下面有一个需要透明的反射效果,我正在使用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();
        }
Run Code Online (Sandbox Code Playgroud)

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

我该怎么办?

编辑:排序,谢谢(Lance McNearney).

现在,如果我必须将其保存到文件怎么办?

另外,奖金问题:

该死的棒球手表http://upload.snelhest.org/images/100219batwatch.jpg

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

Lan*_*ney 6

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

一个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();
}
Run Code Online (Sandbox Code Playgroud)