我正在摆弄桌面小工具(时钟)。它下面有一个需要透明的反射效果,我正在使用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
在表单下方的选项和图标是一个小烦恼,如果可能的话,我希望它们最终位于顶部或下方(保持平滑的抗锯齿)。我认为这样可以解决我的问题。
答案 0 :(得分: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();
}