explorations,

Writing code to take a screenshot

Gerald Haslhofer Gerald Haslhofer Follow May 16, 2020 · 1 min read
Writing code to take a screenshot
Share this

What does it take on Windows to take a screenshot and save it as an image file

Questions: which development environment and language?

Options:

  • regular .NET. Pros: familiarity, lots of documentation
  • .NET core. Pros: future proof. learn something new
  • Python. Not sure if even possible, but enables workflow into AI

Development enviornment:

  • Visual Studio Code. Lightweight. Future proof. Learning curve
  • Full blown Visual Studio. Familiar

Data points:

  • [.net core supported with VS code] on official MS documentation (https://docs.microsoft.com/en-us/dotnet/core/tutorials/with-visual-studio-code)

Decision: use .dot net Core with Visual Studio Code.

Setting up the development environment

  • Official instructions on docs
  • Started using .Net Core 3.1.202

Create git repo

Location on Git: https://github.com/haslhofer/screencapture

Create Hello World .Net Core app

Instructions: https://docs.microsoft.com/en-us/dotnet/core/tutorials/with-visual-studio-code

Find the right APIs for Screen capture in Windows

Option 1: PrintWindow

add package to dot net core:

dotnet add package System.Drawing.Common

Issues along the way:

  • Monitor scaling. Fix:

    call User32.SetProcessDPIAware();

  • Multiple monitors: Article that describes doing this in C++ here

Capturing mouse and keyboard events Article to hook input:

Need to understand Windows GDI better:

  • GDI introduction Virtual screen: The bounding rectangle of all the monitors is the virtual screen source
    • Each physical display is represented by a monitor handle of type HMONITOR.
    • Any function that returns a display device context (DC) normally returns a DC for the primary monitor
    • To obtain the DC for another monitor, use the EnumDisplayMonitors function
    • Or, you can use the device name from the GetMonitorInfo function to create a DC with CreateDC.