Screenshot code for the specified area of the screen based on the C implementation

  • 2020-06-01 10:51:20
  • OfStack

Previously seen in other places one by calling system API screenshots implementation examples, from the heart, I don't like this happens in C # code, now what all speak "harmony", I think this kind of practice is the "harmony" spoiled our code, ha ha, joke, sometimes, not through the system API really hard to do.

Here's how to get a screenshot using pure C#. The code is as follows:


// Capture a full screen image 
        private void btnFullScreen_Click(object sender, EventArgs e)
        {
            // Create an image and save the captured image 
            Bitmap image = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            Graphics imgGraphics = Graphics.FromImage(image);
            // Set the screenshot area   KeLeYi 
            imgGraphics.CopyFromScreen(0, 0, 0, 0, new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height));
            // save 
            SaveImage(image);
        }
// Save image file 
        private void SaveImage(Image image)
        {
            if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string fileName = saveFileDialog.FileName;
                string extension = Path.GetExtension(fileName);
                if (extension == ".jpg")
                {
                    image.Save(fileName, ImageFormat.Jpeg);
                }
                else
                {
                    image.Save(fileName, ImageFormat.Bmp);
                }
            }
        }

This code is simple enough to set the location and size of the screenshot area, but it only works in a.net 2.0 environment. I am in the next step to improve the implementation of drag drag screenshot technology, similar to QQ screenshot method, is tackling the key, if you complete the release of the source code.


Related articles: