Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save print screen in C#

Tags:

c#

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:\Temp\printscreen.jpg", ImageFormat.Jpeg);

This is my code for print screen button. The problem is that I press the button few times and it just write over the old image file (printscreen.jpg), it will not create another new image file such as printscreen1.jpg.

like image 627
user2650977 Avatar asked Aug 04 '13 18:08

user2650977


People also ask

Where is Print Screen save?

Pressing PRINT SCREEN captures an image of your entire screen and copies it to the Clipboard in your computer's memory. You can then paste (CTRL+V) the image into a document, email message, or other file.

How do you save a screenshot in Terminal?

Alt + PrtSc – Save a screenshot of the current window to Pictures. Ctrl + PrtSc – Copy the screenshot of the entire screen to the clipboard. Shift + Ctrl + PrtSc – Copy the screenshot of a specific region to the clipboard.

Which is Print Screen key?

Depending on your hardware, you may use the Windows Logo Key + PrtScn button as a shortcut for print screen. If your device does not have the PrtScn button, you may use FnFnKeys with an Fn key or F Lock provide two sets of commands for many keys. This includes the top row of standard function keys (F1–F12). Standard commands are labeled on the front of the keys (such as F3). Alternate commands are labeled on top of the keys (such as Redo).https://support.microsoft.com › en-us › topicHow do I use the Fn key/F Lock key/Alternate command keys? + Windows logo key + Space Bar to take a screenshot, which can then be printed.


2 Answers

Try this. It will generate a unique file each time.

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:\Temp\printscreen"+Guid.NewGuid()+".jpg", ImageFormat.Jpeg);

OR

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);
var date = DateTime.Now.ToString("MMddyyHmmss");
bitmap.Save(@"C:\Temp\printscreen"+date+".jpg", ImageFormat.Jpeg);
like image 114
Chris Kooken Avatar answered Oct 10 '22 13:10

Chris Kooken


it will not create another new image file

You are not asking it to create one, your code

bitmap.Save(@"C:\Temp\printscreen.jpg", ImageFormat.Jpeg);

is always writing to the same file.

new image file such as printscreen1.jpg

If you want to create a new file, you need to generate your filename dynamically. Something like

 string fileName = System.IO.Path.GetTempPath() + DateTime.Now.ToString() + ".jpg";
 bitmap.Save(fileName, ImageFormat.Jpeg);
like image 6
Ehsan Avatar answered Oct 10 '22 13:10

Ehsan