Screenshot: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Screenshot
| Old | New | Line | |
|---|---|---|---|
| 1 | + | These code examples create the screenshot in the TBitmap object. You can then save this bitmap to a file. | |
| 2 | + | ||
| 3 | + | ## Example 1 | |
| 4 | + | ||
| 5 | + | This page has the cross-platform example: [[Developing with Graphics|Developing with Graphics#Taking a screenshot of the screen]]. | |
| 6 | + | ||
| 7 | + | This takes the whole desktop area (all monitors) and puts it into a TBitmap. | |
| 8 | + | ||
| 9 | + | ## Example 2 | |
| 10 | + | ||
| 11 | + | Code from forum user *Manlio*. It works on Windows. It gives blank-area bitmap on Linux GTK2, if you replace "GetDesktopWindow" to 0. | |
| 12 | + | ||
| 13 | + | With Monitor=-1 it takes the whole desktop area, otherwise it takes only specified monitor. | |
| 14 | + | ||
| 15 | + | ``` pascal | |
| 16 | + | uses | |
| 17 | + | Graphics, LCLType, LCLIntf; | |
| 18 | + | ||
| 19 | + | procedure ScreenshotToFile(const Filename: string; Monitor: integer); | |
| 20 | + | var | |
| 21 | + | BMP: Graphics.TBitmap; | |
| 22 | + | ScreenDC: HDC; | |
| 23 | + | M: TMonitor; | |
| 24 | + | W, H, X0, Y0: integer; | |
| 25 | + | begin | |
| 26 | + | // Initialize coordinates of full composite area | |
| 27 | + | X0 := Screen.DesktopLeft; | |
| 28 | + | Y0 := Screen.DesktopTop; | |
| 29 | + | W := Screen.DesktopWidth; | |
| 30 | + | H := Screen.DesktopHeight; | |
| 31 | + | // Monitor=-1 takes entire screen, otherwise takes specified monitor | |
| 32 | + | if (Monitor >= 0) and (Monitor < Screen.MonitorCount) then begin | |
| 33 | + | M := Screen.Monitors[Monitor]; | |
| 34 | + | X0 := M.Left; | |
| 35 | + | Y0 := M.Top; | |
| 36 | + | W := M.Width; | |
| 37 | + | H := M.Height; | |
| 38 | + | end; | |
| 39 | + | // prepare the bitmap | |
| 40 | + | BMP := Graphics.TBitmap.Create; | |
| 41 | + | BMP.Width := W; | |
| 42 | + | BMP.Height := H; | |
| 43 | + | BMP.Canvas.Brush.Color := clWhite; | |
| 44 | + | BMP.Canvas.FillRect(0, 0, W, H); | |
| 45 | + | ScreenDC := GetDC(GetDesktopWindow); | |
| 46 | + | // copy the required area: | |
| 47 | + | BitBlt(BMP.Canvas.Handle, 0, 0, W, H, ScreenDC, X0, Y0, SRCCOPY); | |
| 48 | + | ReleaseDC(0, ScreenDC); | |
| 49 | + | // save to file (possibly to TStream, etc.) | |
| 50 | + | BMP.SaveToFile(Filename); | |
| 51 | + | BMP.Free; | |
| 52 | + | end; | |
| 53 | + | ``` | |
| 54 | + | ||
| 55 | + | --- | |
| 56 | + | *From the Free Pascal wiki, page "Screenshot", as it stood on 2022-11-15 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* | |
| 57 | + | Category: Code |
