Code Examples

Screen capture


In this code example, we will show you how to make a screen capture and to save it as image file. One example is written in C#, and another in Python.

Take screenshot using C#

    public static void CaptureMyScreen()
        // IMPORTANT:
        // You must install the System.Drawing.Common package via NuGet first.
        // Tools>NuGet PackagaManager>Package manager console
        // NuGet\Install-Package System.Drawing.Common -Version 7.0.0
        // *****
        // NOTE: This code works only on Windows OS and it is not cross-platform
        // *****

        // define final image size
        using var bitmap = new Bitmap(1920, 1080);
        using (var g = Graphics.FromImage(bitmap))
            // capture screen
            g.CopyFromScreen(0, 0, 0, 0,
            bitmap.Size, CopyPixelOperation.SourceCopy);

        // save image
        bitmap.Save(@"D:\Projects\Csharp\tmp\filename.jpg", ImageFormat.Jpeg);

Take screenshot using Python

This program make a screenshot and saves the file as png image

import pyautogui

myScreenshot = pyautogui.screenshot()'D:\Projects\Python\tmp\screenshot_1.png')