Some of you have seen my screen capture program Gotcha. Well, here is how some of the source for the basic screen shot taking.
First add a picture box to your form and name it PictureBox1. Next, add a button with any name you want. Now we are ready for the code. VB.NET users, put this under the button’s click event:
Dim bounds As Rectangle Dim screenshot As System.Drawing.Bitmap Dim graph As Graphics bounds = Screen.PrimaryScreen.Bounds screenshot = New System.Drawing.Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb) graph = Graphics.FromImage(screenshot) graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy) PictureBox1.Image = screenshot
C# users put this under the button’s click event:
Rectangle bounds; System.Drawing.Bitmap screenshot; Graphics graph; bounds = Screen.PrimaryScreen.Bounds; screenshot = new System.Drawing.Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); graph = Graphics.FromImage(screenshot); graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy); PictureBox1.Image = screenshot;
For saving your screenshots just add another button to the form. Add this code for VB.NET users:
Dim SaveFileDialog1 As New SaveFileDialog() SaveFileDialog1.Filter = "PNG|*.png|GIF|*.gif" If SaveFileDialog1.ShowDialog() = DialogResult.OK Then If Not PictureBox1.Image Is Nothing Then PictureBox1.Save(SaveFileDialog1.FileName) End If End If
C# users use this instead:
SaveFileDialog SaveFileDialog1 = new SaveFileDialog(); SaveFileDialog1.Filter = "PNG|*.png|GIF|*.gif"; if (SaveFileDialog1.ShowDialog() == DialogResult.OK) { if (PictureBox1.Image != null) { PictureBox1.Save(SaveFileDialog1.FileName); } }
That’s it folks, have fun!
