Last active
February 12, 2023 12:31
-
-
Save 7shi/69cb2db70a19449982457e19bed3c246 to your computer and use it in GitHub Desktop.
[F#] Screen Capture
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if NETF | |
#r "System" | |
#r "System.Drawing" | |
#r "System.Windows.Forms" | |
#else | |
//#r "nuget: System.Drawing.Common, 5.0.2" | |
#r @"C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App\6.0.4\System.Drawing.Common.dll" | |
#r @"C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App\6.0.4\System.Windows.Forms.dll" | |
#r @"C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App\6.0.4\System.Windows.Forms.Primitives.dll" | |
#r @"C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App\6.0.4\Microsoft.Win32.SystemEvents.dll" | |
#endif | |
open System.Runtime.InteropServices | |
[<DllImport("user32.dll")>] extern nativeint GetDesktopWindow() | |
let SRCCOPY = 0x00cc0020 | |
[<DllImport("gdi32.dll")>] extern bool BitBlt( | |
nativeint hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, | |
nativeint hdcSrc, int nXSrc, int nYSrc, int dwRop) | |
open System.Drawing | |
let captureScreen (r: Rectangle) = | |
if r.X > 10000 || r.Y > 10000 || r.Width > 5000 || r.Height > 5000 || r.Width < 1 || r.Height < 1 then new Bitmap(1, 1) else | |
let ret = new Bitmap(r.Width, r.Height) | |
let desktop = GetDesktopWindow() | |
use g1 = Graphics.FromImage ret | |
use g2 = Graphics.FromHwnd desktop | |
g1.Clear(Color.Black) | |
let hdc1 = g1.GetHdc() | |
let hdc2 = g2.GetHdc() | |
ignore <| BitBlt(hdc1, 0, 0, r.Width, r.Height, hdc2, r.X, r.Y, SRCCOPY) | |
g1.ReleaseHdc(hdc1) | |
g2.ReleaseHdc(hdc2) | |
ret | |
open System.Drawing.Imaging | |
open System.Windows.Forms | |
do | |
use bmp = captureScreen(Screen.PrimaryScreen.Bounds) | |
bmp.Save("screen.png", ImageFormat.Png) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment