2014-12-19 47 views
2

好吧,发生这种情况时,我正在编写一个程序来拍摄一些截图,并且在处理另一个进程正在使用的文件时遇到了一些困难,希望有人可以帮助我找到一种方法来“关闭”这个过程或让我知道如何继续。创建和删除图片/屏幕截图

//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

你似乎没有关闭位图,我想像可能出现有错误,但在那里你真正得到它是​​不明错误 – Sayse 2014-12-19 07:16:05

+0

@Sayse http://gyazo.com/4b50945b0d157d082f7897e34a705560该程序只需要截图并将其保存到该文件夹​​中。在获取这些截图之后,我想删除它们,那是错误出现时,我尝试过Dispose方法,该如何关闭位图? – abr 2014-12-19 07:25:35

+0

我的意思是处置,但你最好使用'using'块。 – Sayse 2014-12-19 07:53:25

回答

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”文件夹的文件夹权限或您要保存的任何位置。

+0

不拥有.Close方法:x – abr 2014-12-19 08:07:31

+0

我编辑了答案。试试看。 – Phoenix 2014-12-19 08:39:32

+0

仍然给我完全相同的错误,我也只是测试评论创建一个新的位图区域,它给出了同样的错误,任何想法?感谢您到目前为止的帮助 – abr 2014-12-19 09:24:02

1

您可以配置()的位图.....

bmpScreenshot.Dispose(); 
+0

这就是使用语句所做的......并且尽可能使用通用语句更好。 – 2014-12-19 14:51:04

+0

我在使用声明中看不到OP! – SuncoastOwner 2014-12-19 14:56:46

+0

对,我指的是一个声明,但也是以前的答案。 – 2014-12-19 15:39:59