2013-04-21 37 views
-1

我可以在定時器上保存屏幕截圖,但是如何將它保存爲新名稱並且不會每次都覆蓋?每次保存爲新名稱

Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, 
          Screen.PrimaryScreen.Bounds.Height); 

Graphics graphics = Graphics.FromImage(bitmap as Image); 

graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size); 

bitmap.Save(@"c:tempscreenshot.bmp", ImageFormat.Bmp); 
+0

可能是你可以在名稱附加當前日期時間試試。 – Sachin 2013-04-21 22:36:48

+0

你不需要'像位圖一樣'。只需使用「位圖」。 – Ryan 2013-04-21 22:42:23

回答

3

您只需要每次生成一個唯一的名稱。有幾種可能性。之一將是在末尾添加一個日期時間字符串:

Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, 
       Screen.PrimaryScreen.Bounds.Height); 

Graphics graphics = Graphics.FromImage(bitmap as Image); 

    graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size); 

bitmap.Save(@"c:tempscreenshot" + DateTime.Now.Ticks + ".bmp", ImageFormat.Bmp); 
+0

非常感謝你,我真的很感激。你讓我今天一整天都感覺很好。如果我可以做任何事情來報答你,只要說出來。在Twitter上關注? iTunes的審查?再次感謝 – user2305454 2013-04-21 22:48:52

+0

沒問題,樂意幫忙。如果你有興趣,這是我的推特處理:@Kennethtruyers – Kenneth 2013-04-21 22:51:16

3

使用一個整數(或其他數值類型)和你的計時器內加一。然後用像打電話給你的保存方法:

bitmap.Save(string.Format("c:tempscreenshot.{0}.bmp", counter), ImageFormat.Bmp); 

或使用GUID:

bitmap.Save(string.Format("c:tempscreenshot.{0}.bmp", Guid.NewGuid().ToString("N")), ImageFormat.Bmp); 
5

可以使用方便的方法:Path.GetRandomFileName()

Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, 
       Screen.PrimaryScreen.Bounds.Height); 

Graphics graphics = Graphics.FromImage(bitmap as Image); 

    graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size); 

bitmap.Save("c://" + Path.GetRandomFileName() + ".bmp", ImageFormat.Bmp); 
1

您可以生成新的文件名,每次基於當前時間。事情是這樣的:

string GenerateFilename() { 
    string file = DateTime.Now.ToString("yy.MM.dd HH.mm.ss") + ".bmp"; 
    return @"C:\" + file; 
} 

關於使用這種方法的好處是,當你瀏覽你在哪裏保存文件的文件夾,它們進行排序。

,然後用它在你的現有代碼:

bitmap.Save(GenerateFilename(), ImageFormat.Bmp); 

您也可以在前面加上任何文本(如image-或東西)的文件名。

另一種選擇是將一個整數附加到文件名的末尾,就像一些拷貝處理程序一樣。