2014-12-19 49 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