Skip to content

Instantly share code, notes, and snippets.

@notcoffeetable
Created April 24, 2013 18:06
Show Gist options
  • Save notcoffeetable/5454182 to your computer and use it in GitHub Desktop.
Save notcoffeetable/5454182 to your computer and use it in GitHub Desktop.
Screenshot screen by index. Crop function is a free bonus.
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
class Program
{
static Bitmap screenshot;
static Graphics gfxScreenshot;
[STAThreadAttribute]
static void Main(string[] args)
{
while (true)
{
Console.WriteLine("Press enter to take shot");
Console.ReadLine();
Screen screen = Screen.AllScreens[1];
screenshot = new Bitmap(screen.Bounds.Width,
screen.Bounds.Height,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
// Create a graphics object from the bitmap
gfxScreenshot = Graphics.FromImage(screenshot);
// Take the screenshot from the upper left corner to the right bottom corner
gfxScreenshot.CopyFromScreen(
screen.Bounds.X,
screen.Bounds.Y,
0,
0,
screen.Bounds.Size,
CopyPixelOperation.SourceCopy);
string savePath = String.Format(@"C:\test.jpeg");
screenshot.Save(savePath, ImageFormat.Jpeg);
}
}
static Image CropImage(Image toBeCropped, RectangleF cropBounds)
{
Bitmap b = toBeCropped as Bitmap;
Bitmap target = new Bitmap( Convert.ToInt16(cropBounds.Width), Convert.ToInt16(cropBounds.Height));
using (Graphics g = Graphics.FromImage(target))
{
g.DrawImage(b, new RectangleF(0, 0, target.Width, target.Height), cropBounds, GraphicsUnit.Pixel);
}
return target;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment