2010-02-19 101 views
2

我摆弄桌面小工具(时钟)。它下面有反射效果,需要透明,我使用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设置为其他内容,但那不起作用。

我该怎么办?

编辑:分类,谢谢(兰斯麦克尼尔尼)。

现在,如果我已将保存到文件,该怎么办?

而且,奖金问题:

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

选项和图标的形式下结束了是一个小麻烦,如果可能的话,我想他们要么结束在上面,或低于(保持平滑的抗锯齿)。我假设这是摆脱我的束缚,以解决问题的方法。

回答

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(); 
} 
+0

咄。谢谢。 稍后标记您的答案,允许人们对第二部分发表评论 – pastapockets 2010-02-19 18:41:30

相关问题