Skip to content

Instantly share code, notes, and snippets.

@espio999
Last active December 4, 2021 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save espio999/94e33d3077257162d764ca83cc2ab2fd to your computer and use it in GitHub Desktop.
Save espio999/94e33d3077257162d764ca83cc2ab2fd to your computer and use it in GitHub Desktop.
screenshot of whole screen and active window with .NET Framework
String filename(String val)
{
String my_dir = "d:\\user temp\\20211204\\";
String my_file = DateTime.Now.ToString("hhmmss") + ".jpg";
return (my_dir + val + my_file);
}
Size screen_size = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Size window_size = this.Size;
var screen_bmp = new Bitmap(screen_size.Width, screen_size.Height);
var window_bmp = new Bitmap(window_size.Width, window_size.Height);
var screen_shot = Graphics.FromImage(screen_bmp);
var window_capture = Graphics.FromImage(window_bmp);
screen_shot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, screen_size);
window_capture.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, window_size);
screen_bmp.Save(filename("screen"), System.Drawing.Imaging.ImageFormat.Jpeg);
window_bmp.Save(filename("window"), System.Drawing.Imaging.ImageFormat.Jpeg);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment