- This topic explains about how to capture screenshot from desktop and crop it in C#.
- Here we need only a PictureBox control in C# to capture screenshot from Desktop and crop it.
- When we first click the Button, it Capture screenshot from Desktop.
- Then again we click the Button; it crops the captured screenshot from Desktop into size that we specified.
PictureBox Class – Capture screenshot from Desktop and crop it in C#
- PictureBox class represents a Windows picture box control for displaying an image.
Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
- Syntax
|
1 2 3 4 5 |
[DockingAttribute(DockingBehavior.Ask)] [ComVisibleAttribute(true)] [ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] [DefaultBindingPropertyAttribute("Image")] public class PictureBox : Control, ISupportInitialize |
Code to display an Image:
|
1 |
PictureBox1.Image == Image.FromFile(strFileName) |
- Through the above code we are loading image to the Image Property of the PictureBox Control.
Bitmap Class – Capture screenshot from Desktop and crop it in C#
- Encapsulates a GDI+ bitmap, which consists of the pixel data for a graphics image and its attributes. A Bitmap is an object used to work with images defined by pixel data.
Capture screenshot from Desktop and crop it in C# – Complete Code
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace textc { public partial class Form1 : Form { bool FirstTime = true; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Bitmap bm = new Bitmap(pictureBox1.Width, pictureBox1.Height); using (Graphics g = Graphics.FromImage(bm)) { g.DrawString("Click on the Button to see the screen", Font, Brushes.Black, new Point(10, 10)); } pictureBox1.Image = bm; } private void button1_Click(object sender, EventArgs e) { switch (FirstTime) { case true: Bitmap bm = new Bitmap(Width, Height); using (Graphics g = Graphics.FromImage(bm)) { g.CopyFromScreen(new Point(0, 0), new Point(0, 0), Size); g.DrawString("Click again to see it cropped", Font, Brushes.Black, new Point(10, 10)); } pictureBox1.Image = bm; FirstTime = false; break; case false: Bitmap bm1 = new Bitmap(pictureBox1.Image); Bitmap bs = new Bitmap(bm1, Convert.ToInt32(bm1.Width * 0.5), Convert.ToInt32(bm1.Height * 0.5)); pictureBox1.Image = bs; break; } } } } |

Capture screenshot from Desktop and crop it in C# – Capture 1

Capture screenshot from Desktop and crop it in C# – Capture and cropped
Capture screenshot from Desktop and crop it in C#





