8
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.

Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
user2650977
  • 103
  • 2
  • 2
  • 4

6 Answers6

9

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);
Chris Kooken
  • 32,730
  • 15
  • 85
  • 123
6

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);
Ehsan
  • 31,833
  • 6
  • 56
  • 65
3

You could count how many files already exist in the directory that start with printscreen.

int count =  Directory.EnumerateFiles(@"C:\Temp\").Where(x => x.StartsWith("printscreen")).Count();
bitmap.Save(String.Format(@"C:\Temp\printscreen{0}.jpg", count), ImageFormat.Jpeg);

This might be slower if you have a lot of files since it has to iterate through them but it's more automated.

Another option would be to create a file that stores the current counter number.

File.Write("Counter.txt", counter.ToString());

Then when you want to start the counter after your program restarts, you can do this.

int counter = 0;
if(File.Exists("Counter.txt"))
   counter = Int32.Parse(File.ReadAllText("Counter.txt"));

bitmap.Save(String.Format(@"C:\Temp\printscreen{0}.jpg", ++counter), ImageFormat.Jpeg);

Of course, if you don't plan on restarting the app and continuing the sequence, you can avoid the storing of the counter in a text file and just increment the counter from zero.

keyboardP
  • 68,824
  • 13
  • 156
  • 205
1

Create a field say counter and increment it on every button click and use it for file name -

bitmap.Save(@"C:\Temp\printscreen" + (counter++) + ".jpg", ImageFormat.Jpeg);
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
1

You can also try like this:

int number;
string[] path = Directory.GetFiles(@"C:\Temp\", @"PrintScreen*");

if (path.Length == 0)
{    bitmap.Save(@"C:\Temp\printscreen.jpg", ImageFormat.Jpeg); return;  }

Array.Sort(path);
Array.Reverse(path);

int.TryParse(Regex.Match(path[0], @"(\d+)(?=\.jpg)").ToString(), out number);
bitmap.Save(@"C:\Temp\printscreen" + (number + 1).ToString() + ".jpg", ImageFormat.Jpeg);

I think it would be more efficient to pick the last file by sorting it in descending order rather than enumerating over them. Also, no need to keep track of separate counter pick the last count and increment it.

NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
1
                   {
                    //Application.DoEvents();
                    Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
                    Graphics graphics = Graphics.FromImage(printscreen as Image);
                    graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size);
                    string path = "";
                    if (forLog)
                    {
                        path = LoggerPath + StudentNumber.ToString() + "\\" + SessionID.ToString() + "\\";
                    }
                    else
                    {
                        path = PrintScreenPath + StudentNumber.ToString() + "\\" + SessionID.ToString() + "\\";
                    }
                    if (!Directory.Exists(path))
                        Directory.CreateDirectory(path);
                    string fileName = DateTime.Now.Ticks.ToString();
                    SaveJPGWithCompressionSetting(printscreen, path + fileName + ".jpeg", 17L);
                    printscreen.Dispose();
                    graphics.Dispose();
                    return fileName;
                }

And ere is SaveJPGWithCompressionSetting metHod

   private void SaveJPGWithCompressionSetting(Image image, string szFileName, long lCompression)
    {
            try
            {
                EncoderParameters eps = new EncoderParameters(1);
                eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, lCompression);
                ImageCodecInfo ici = GetEncoderInfo("image/jpeg");
                image.Save(szFileName, ici, eps);
            }

}

Baso
  • 1,354
  • 4
  • 19
  • 41