Skip to content

Instantly share code, notes, and snippets.

@hugo-dc
Created February 18, 2015 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hugo-dc/8cbe0597e495c5c6f76f to your computer and use it in GitHub Desktop.
Save hugo-dc/8cbe0597e495c5c6f76f to your computer and use it in GitHub Desktop.
Screenshot with Haskell
import Graphics.Win32.Window
import Graphics.Win32.GDI.Bitmap
import Graphics.Win32.GDI.HDC
import Graphics.Win32.GDI.Graphics2D
main = do desktop <- getDesktopWindow -- Grab the Hwnd of the desktop, GetDC 0, GetDC NULL etc all work too
hdc <- getWindowDC (Just desktop) -- Get the dc handle of the desktop
(x,y,r,b) <- getWindowRect desktop -- Find the size of the desktop so we can know which size the destination bitmap should be
-- (left, top, right, bottom)
newDC <- createCompatibleDC (Just hdc) -- Create a new DC to hold the copied image. It should be compatible with the source DC
let width = r - x -- Calculate the width
let height = b - y -- Calculate the Height
newBmp <- createCompatibleBitmap hdc width height -- Create a new Bitmap which is compatible with the newly created DC
selBmp <- selectBitmap newDC newBmp -- Select the Bitmap into the DC, drawing on the DC now draws on the bitmap as well
bitBlt newDC 0 0 width height hdc 0 0 sRCCOPY -- use SRCCOPY to copy the desktop DC into the newDC
createBMPFile "Foo.bmp" newBmp newDC -- Write out the new Bitmap file to Foo.bmp
putStrLn "Bitmap image copied" -- Some debug message
deleteBitmap selBmp -- Cleanup the selected bitmap
deleteBitmap newBmp -- Cleanup the new bitmap
deleteDC newDC -- Cleanup the DC we created.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment