Get screenshot programmatically

By | February 22, 2021

Several years ago I implemented a small project: “Optical character recognition (OCR) of Windows application screenshots”. It the first part of the project – the console application which makes screenshot of some process main window or capture entire screen image. The process is selected by its process ID which should specified as argument. When argument is -1 the screenshot of full screen image is captured. The screenshot is saved as JPEG file in c:\temp directory.
The code (download):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace GetWindowImage
   class Program
      [DllImport("User32.dll", SetLastError = true)]
      static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags);

      private static extern IntPtr GetWindowRect(IntPtr WindowHandle, ref Rect rect);

      private struct Rect
         public int Left;
         public int Top;
         public int Right;
         public int Bottom;

      static void Main(string[] args)
         Bitmap bitmap = null;
         Graphics graphics = null;
         int pid = -2;
         string fileName = "";
         if(args.Length < 1)
            Console.WriteLine("Specify process ID or -1 for PrtScn");
            pid = Convert.ToInt32(args[0]);
            Console.WriteLine("The argument should be integer");
            if (pid == -1)
               // get full screen
               bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
               fileName = "printscreen" + DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
               graphics = Graphics.FromImage(bitmap as Image);
               graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
            } else
               Process process = Process.GetProcessById(pid);
               fileName = process.ProcessName + "-pid_" + pid.ToString() + "_" + DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
               IntPtr hwnd = process.MainWindowHandle;
               Rect rect = new Rect();
               GetWindowRect(hwnd, ref rect);
               bitmap = new Bitmap(rect.Right – rect.Left, rect.Bottom – rect.Top);

               graphics = Graphics.FromImage(bitmap);
               IntPtr dc = graphics.GetHdc();
               bool success = PrintWindow(hwnd, dc, 0);
            bitmap.Save(@"C:\Temp\" + fileName + ".jpg", ImageFormat.Jpeg);
         } catch(Exception ex)
            Console.WriteLine("Exception: " + ex.Message);

Get screenshot of calc.exe (start calc.exe, find PID of cacl.exe process, capture screenshot):
Capture image of calc process
Captured screenshot
Links for OCR processing:
Google Cloud Vision API

Microsoft Azure Computer Vision

OCR Space

Leave a Reply

Your email address will not be published. Required fields are marked *