Location Earth, (I Think)
Home
About Us
Contact Us
Links
Home
Taking a Desktop and/or Window Screenshot
Requires GDI32.dll & User32.DLL
 
Example 1 - Using GetPixel
 
In order to take a Screenshot of the Desktop, or any other window, We need access to GetPixel which is located in "GDI32.dll", This is Windows version or the DarkBasic Point(x,y) Command to get the color of a Pixel, but based on the coordinates of a Windows Handle.
 
This Example will get the Color under the Mouse, and Color a Cube in that color.

 

As the function this uses is based on  Screen Coordinates, I've also included Functions for CursorX() and CursorY(), To get more information on this, or to get more Cursor Commands check Advanced Cursor Commands

` ----------------------------------------------------------------------

` Screenshot Example 1 - Using GetPixel

`

` By Michael Mihalyfi

` Mihalyfi@Hotmail.com

`

` Fell Free to use this code as you see fit, but if you do then

` a mension in your Credits would be nice.

` ----------------------------------------------------------------------

  GLOBAL DLL_User32 AS INTEGER : DLL_User32 = 1 : LOAD DLL "User32.dll",DLL_User32

  GLOBAL DLL_GDI32  AS INTEGER : DLL_GDI32  = 2 : LOAD DLL  "GDI32.dll",DLL_GDI32

  SET WINDOW ON

 

  MAKE OBJECT CUBE 1,10

 

   DO
   NewColor = GetPixel(CursorX(),CursorY())
IF NewColor <> Color THEN Color = NewColor : COLOR OBJECT 1,Color
   ROTATE OBJECT 1,OBJECT ANGLE X(1)+0.5,OBJECT ANGLE Y(1)+0.5,OBJECT ANGLE Z(1)+0.5
   SYNC :
LOOP

 

`        ----------------------------------------------------------------------

FUNCTION GetPixel(X,Y)

         hDsk  = CALL DLL(DLL_User32,"GetDesktopWindow")
         hDc   = CALL DLL(DLL_User32,"GetWindowDC",hDsk)

         Color = CALL DLL(DLL_GDI32,"GetPixel",hDc,xPos,yPos)
         Color = RGB(RGBB(Color),RGBG(Color),RGBR(Color))

ENDFUNCTION Color

`        ----------------------------------------------------------------------

FUNCTION CursorX()                                            : ` Get the X Position of the Cursor
        
Memblock = FindFreeMemblock()
         MAKE MEMBLOCK Memblock,20
         Temp   = CALL DLL(DLL_User32,"GetCursorPos",GET MEMBLOCK PTR(Memblock))
         Result = MEMBLOCK DWORD(Memblock,0)
         DELETE MEMBLOCK Memblock

 

ENDFUNCTION Result

FUNCTION CursorY()                                            : ` Get the Y Position of the Cursor
       
  Memblock = FindFreeMemblock()
         MAKE MEMBLOCK Memblock,20
         Temp   = CALL DLL(DLL_User32,"GetCursorPos",GET MEMBLOCK PTR(Memblock))
         Result = MEMBLOCK DWORD(Memblock,4)
         DELETE MEMBLOCK Memblock

 

ENDFUNCTION Result

`        ----------------------------------------------------------------------

FUNCTION FindFreeMemblock() : ` INTERNAL USE ONLY...
  
Local Result As Integer
         Result = 1
         Repeat
        
Inc Result,1
         Until MemBlock Exist(Result) = 0

ENDFUNCTION Result

`        ----------------------------------------------------------------------

 
Example 2 - Taking A ScreenShot.
 
This Example below contains 4 functions
  • Snapshot() which will take a FullScreen Picture of the Desktop and return the Image Number
  • SnapshotArea(X,Y,Width,Height) will take a Screenshot of an Area of the Desktop
  • SnapshotWindow(Handle) will take a Screenshot of a Single Window.
  • SnapshotWindowArea(Handle,x,y,Width,Height) will take a Screenshot of an Area of a Single Window.

 

This Example will take a Screenshot, And Texture a Cube with the Image

` ----------------------------------------------------------------------

` Screenshot Example 2 - Taking A Screenshot

`

` By Michael Mihalyfi

` Mihalyfi@Hotmail.com

`

` Fell Free to use this code as you see fit, but if you do then

` a mension in your Credits would be nice.

` ----------------------------------------------------------------------

  GLOBAL DLL_User32 AS INTEGER : DLL_User32 = 1 : LOAD DLL "User32.dll",DLL_User32

  GLOBAL DLL_GDI32  AS INTEGER : DLL_GDI32  = 2 : LOAD DLL  "GDI32.dll",DLL_GDI32

  SET WINDOW ON : Sync

 

  MAKE OBJECT CUBE 1,10

 

   DO : CLS

   TEXT 10,10,"Press 1 to take a Desktop Screenshot"

   TEXT 10,30,"Press 2 to take a Screenshot of the Window Under the Mouse"

   TEXT 10,50,"Press 3 to take a Screenshot of Screen Area 100,100,500,500"

   I$  = INKEY$()

IF I$  = "1" THEN NewImg = Screenshot()

IF I$  = "2" THEN NewImg = ScreenshotWindow(WindowAtPoint(CursorX(),CursorY()))

IF I$  = "3" THEN NewImg = ScreenshotArea(100,100,500,500)

IF NewImg <> Img THEN TEXTURE OBJECT 1,NewImg : DELETE IMAGE Img : Img = NewImg
  
ROTATE OBJECT 1,OBJECT ANGLE X(1)+0.5,OBJECT ANGLE Y(1)+0.5,OBJECT ANGLE Z(1)+0.5
   SYNC : LOOP

 

 

`        ----------------------------------------------------------------------

FUNCTION Snapshot()

         hDsk   = CALL DLL(DLL_User32,"GetDesktopWindow")
         hDc   
= CALL DLL(DLL_User32,"GetWindowDC",hDsk)

         Width  = CALL DLL(DLL_User32,"GetSystemMetrics",0)
         Height = CALL DLL(DLL_User32,"GetSystemMetrics",1)

         Img    = SnapshotWindowArea(hDc,0,0,Width,Height)

 

ENDFUNCTION Img

FUNCTION SnapshotArea(x,y,Width,Height)

         hDsk   = CALL DLL(DLL_User32,"GetDesktopWindow")
         hDc   
= CALL DLL(DLL_User32,"GetWindowDC",hDsk)
         Img    = SnapshotWindowArea(hDc,x,y,Width,Height)

 

ENDFUNCTION Img

FUNCTION SnapshotWindow(Handle)

         Memblock = FindFreeMemblock()
         MAKE MEMBLOCK Memblock,20
         Temp   = CALL DLL(DLL_User32,"GetWindowRect",Handle,GET MEMBLOCK PTR(Memblock))
         Width  = MEMBLOCK DWORD(Memblock,8)

         Height = MEMBLOCK DWORD(Memblock,8)
         DELETE MEMBLOCK Memblock

         Img    = SnapshotWindowArea(Handle,0,0,Width,Height)

 

ENDFUNCTION Img

FUNCTION SnapshotWindowArea(Handle,x,y,Width,Height)

         Img    = FindFreeImage()

         Bmp    = FindFreeBitmap()

         OldBmp = CURRENT BITMAP()

         CREATE BITMAP Bmp,Width,Height
         SET CURRENT BITMAP Bmp
         LOCK PIXELS
         FOR yPos = y to Height
         FOR xPos = x to Width
         Color = CALL DLL(DLL_GDI32,"GetPixel",hDc,xPos,yPos)
         INK RGB(RGBB(Color),RGBG(Color),RGBR(Color)),0
         DOT xPos,yPos
         NEXT xPos : NEXT yPos
         UNLOCK PIXELS
         GET IMAGE Img,xPos,yPos,Width,Height,1
         SET CURRENT BITMAP OldBmp
         DELETE BITMAP Bmp

 

ENDFUNCTION Img

`        ----------------------------------------------------------------------

FUNCTION CursorX()                                            : ` Get the X Position of the Cursor
         Memblock = FindFreeMemblock()
         MAKE MEMBLOCK Memblock,20
         Temp   = CALL DLL(DLL_User32,"GetCursorPos",GET MEMBLOCK PTR(Memblock))
         Result = MEMBLOCK DWORD(Memblock,0)
         DELETE MEMBLOCK Memblock

 

ENDFUNCTION Result

FUNCTION CursorY()                                            : ` Get the Y Position of the Cursor
       
  Memblock = FindFreeMemblock()
         MAKE MEMBLOCK Memblock,20
         Temp   = CALL DLL(DLL_User32,"GetCursorPos",GET MEMBLOCK PTR(Memblock))
         Result = MEMBLOCK DWORD(Memblock,4)
         DELETE MEMBLOCK Memblock

 

ENDFUNCTION Result

`        ----------------------------------------------------------------------

FUNCTION FindFreeMemblock() : ` INTERNAL USE ONLY...
   Local Result As Integer
         Result = 1
         Repeat
         Inc Result,1
         Until MemBlock Exist(Result) = 0

ENDFUNCTION Result

FUNCTION FindFreeImage() : ` INTERNAL USE ONLY...
   Local Result As Integer
         Result = 1
         Repeat
         Inc Result,1
         Until Image Exist(Result) = 0

ENDFUNCTION Result

FUNCTION FindFreeBitmap() : ` INTERNAL USE ONLY...
   Local Result As Integer
         Result = 1
         Repeat
         Inc Result,1
         Until Bitmap Exist(Result) = 0

ENDFUNCTION Result

`        ----------------------------------------------------------------------

 

Example 3 - Taking A Screenshot, (Combine With BlueGui)

 

This next Example is 1 Function SnapshotGadget(GadgetNumber) , This is ONLY Usable if you have BlueGui and will Take a Screenshot of the gadget, Returning the Image Number Used, Just add this Example to the Example above, You do not need to Include Gui.dba, but you will need to run the StartBlue Command.

 

FUNCTION SnapshotGadget(GadgetNumber)

         Handle = GADGETHANDLE(GadgetNumber)

         Width  = GADGETWIDTH(GadgetNumber)

         Height = GADGETHEIGHT(GadgetNumber)

         Img = SnapshotWindowArea(Handle,0,0,Width,Height)

 

ENDFUNCTION Img


This page was last modified on Thursday, 01 May 2008 13:50