好吧,碰巧我正在编写一个程序来拍摄一些截图并且处理已经被另一个进程使用的文件有些困难,希望有人可以帮我找到一种方法来“关闭”这个过程或者让我知道如何继续
//Create a new bitmap.
var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height,
PixelFormat.Format32bppArgb);
// Create a graphics object from the bitmap.
var gfxScreenshot = Graphics.FromImage(bmpScreenshot);
// Take the screenshot from the upper left corner to the right bottom corner.
gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
Screen.PrimaryScreen.Bounds.Y,
0,
0,
Screen.PrimaryScreen.Bounds.Size,
CopyPixelOperation.SourceCopy);
// Save the screenshot to the specified path that the user has chosen.
//bmpScreenshot.Save(nomeScreen + ".jpeg", ImageFormat.Jpeg);
bmpScreenshot.Save(@"c:\temp\"+nomeScreen+".jpeg", ImageFormat.Jpeg);
count++;
nomeScreen = "S"+Convert.ToString(count);
这就是我所拥有的(我知道,糟糕的编程技巧)来拍摄几张截图,但是后来我希望将它们从我存储它们的地方删除
http://gyazo.com/4b50945b0d157d082f7897e34a705560
这是它发生的截图。如何“关闭”位图?到目前为止,程序执行的唯一操作是截取屏幕截图并将其删除。
string pathString = "C:\\temp";
DirectoryInfo d = new DirectoryInfo(pathString);
foreach (var k in d.GetFiles("*.jpeg"))
{
File.Delete(pathString+"\\" + k);
}
答案 0 :(得分:1)
我相信你需要在bmpScreenshot.Save()之后调用bmpScreenshot.Close()。
抱歉,我正在打电话,无法检查它是否已关闭。
试试这个:
//Create a new bitmap.
using(var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height,
PixelFormat.Format32bppArgb))
{
// Create a graphics object from the bitmap.
var gfxScreenshot = Graphics.FromImage(bmpScreenshot);
// Take the screenshot from the upper left corner to the right bottom corner.
gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
Screen.PrimaryScreen.Bounds.Y,
0,
0,
Screen.PrimaryScreen.Bounds.Size,
CopyPixelOperation.SourceCopy);
// Save the screenshot to the specified path that the user has chosen.
//bmpScreenshot.Save(nomeScreen + ".jpeg", ImageFormat.Jpeg);
bmpScreenshot.Save(@"c:\temp\"+nomeScreen+".jpeg", ImageFormat.Jpeg);
}
count++;
nomeScreen = "S"+Convert.ToString(count);
所以我测试了代码,得到了一些有趣的结果。
我有这个:
private void button1_Click(object sender, EventArgs e)
{
string nomeScreen = "screenshot"+new Random().Next();
screenshotPath = Application.StartupPath+"\\" + nomeScreen + ".jpeg";
//Create a new bitmap.
/*using (*/
var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height,
PixelFormat.Format32bppArgb);//)
// {
// Create a graphics object from the bitmap.
/*using (*/
var gfxScreenshot = Graphics.FromImage(bmpScreenshot);//)
//{
// Take the screenshot from the upper left corner to the right bottom corner.
gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
Screen.PrimaryScreen.Bounds.Y,
0,
0,
Screen.PrimaryScreen.Bounds.Size,
CopyPixelOperation.SourceCopy);
//}
// Save the screenshot to the specified path that the user has chosen.
//bmpScreenshot.Save(nomeScreen + ".jpeg", ImageFormat.Jpeg);
bmpScreenshot.Save(screenshotPath, ImageFormat.Jpeg);
// }
}
private void button2_Click(object sender, EventArgs e)
{
File.Delete(screenshotPath);
}
没有任何使用声明,它工作正常,但在我看来,两个注释掉的都是好的。也许你的问题在别处。您是否检查过文件夹的权限。也许你可以尝试保存到应用程序的exe的路径?这可能会解决它。如果是,那么您需要检查" temp"的文件夹权限。文件夹或您尝试保存的任何地方。
答案 1 :(得分:1)
你可以处理()位图......
bmpScreenshot.Dispose();