ScreenGrab(Windows only)

Share your advanced PureBasic knowledge/code with the community.
Pud
User
User
Posts: 29
Joined: Mon Dec 28, 2009 2:23 am

ScreenGrab(Windows only)

Post by Pud »

Code: Select all

;*************************************************************
;I needed to capture images (or parts of) from the screen quickly and invisibly 
;incorporating them into an application.

;I could find nothing suitable, although I must admit I didn’t look that hard as there is 
;nothing like having control of your own code.

;I looked at the Windows API and tinkered with SetCapture etc. but it’s a long time 
;since I did any of that stuff so I decided on a (mostly) Pure PB solution.

;I am no doubt reinventing the wheel (maybe even for this site) but the following I 
;hope is very easy to understand and adapt if useful.

;I would be interested in other solutions.

;Mike

;*************************************************************
EnableExplicit
Global screenwidth,screenheight
Global win,canvas
Global mousedown,screenimage
Global x,y,w,d

Procedure capturescreen() ;Courtesy of this forum
          Define dc,h
          
          screenimage=CreateImage(#PB_Any,screenwidth,screenheight)
          dc=GetDC_(GetDesktopWindow_())
          h=StartDrawing(ImageOutput(screenimage))
          BitBlt_(h,0,0,DesktopWidth(0),DesktopHeight(0),dc,0,0,#SRCCOPY)
          StopDrawing()
          ReleaseDC_(GetDesktopWindow_(),dc)
       
EndProcedure

Procedure showghostscreen()
          
          StartDrawing(CanvasOutput(canvas))
          DrawAlphaImage(ImageID(screenimage),0,0,180) 
          StopDrawing()
          
EndProcedure 

Procedure dosomething(newimage)
          ;Do what you like with the captured image
          ;Below displays the image and puts it in the clipboard
          
          Define centerx=screenwidth/2-w/2
          Define centery=screenheight/2-d/2
          
          StartDrawing(CanvasOutput(canvas))
          DrawingMode(#PB_2DDrawing_Outlined)
          Box(centerx-1,centery-1,w+2,d+2,#Black)
          DrawImage(ImageID(newimage),centerx,centery)
          StopDrawing()
          
          Repeat
            WaitWindowEvent()
            Select EventType()
              Case #PB_EventType_LeftButtonUp,#PB_EventType_Input,
                   #PB_EventType_KeyDown,#PB_EventType_RightButtonDown
                Break
            EndSelect
          ForEver
          
          SetClipboardImage(newimage) 

EndProcedure 

Procedure startdrag()
          
          If Not mousedown
            x=GetGadgetAttribute(canvas,#PB_Canvas_MouseX)
            y=GetGadgetAttribute(canvas,#PB_Canvas_MouseY)
            mousedown=1
          EndIf 
          
EndProcedure

Procedure dodrag()
          
          If mousedown
            StartDrawing(CanvasOutput(canvas))
            DrawingMode(#PB_2DDrawing_Outlined|#PB_2DDrawing_XOr)
            Box(x,y,w,d)
            w=GetGadgetAttribute(canvas,#PB_Canvas_MouseX)-x
            d=GetGadgetAttribute(canvas,#PB_Canvas_MouseY)-y
            Box(x,y,w,d)
            StopDrawing()
          EndIf

EndProcedure

Procedure stopdrag()
          Define newimage
          
          If w>10 And d>10  ;Ignore silly sized images        
            newimage=GrabImage(screenimage,#PB_Any,x,y,w,d) 
            dosomething(newimage)
          EndIf
          
EndProcedure

Procedure imagecapture()
        
          ExamineDesktops()
          screenwidth=DesktopWidth(0) 
          screenheight=DesktopHeight(0)
          capturescreen()
          
          win=OpenWindow(#PB_Any,0,0,screenwidth,screenheight,"",
                         #PB_Window_BorderLess|#PB_Window_Invisible)
          canvas=CanvasGadget(#PB_Any,0,0,screenwidth,screenheight,#PB_Canvas_Keyboard)
          showghostscreen()
          
          SetActiveGadget(canvas)
          HideWindow(win,0)
          Repeat
            Select WaitWindowEvent()
              Case #PB_Event_Gadget
                Select EventType()
                  Case #PB_EventType_LeftButtonDown : startdrag()
                  Case #PB_EventType_MouseMove : dodrag()
                  Case #PB_EventType_LeftButtonUp : stopdrag() : Break
                  Case #PB_EventType_Input,#PB_EventType_KeyDown,#PB_EventType_RightButtonDown : Break
                EndSelect
            EndSelect
          ForEver

EndProcedure 
imagecapture()
infratec
Always Here
Always Here
Posts: 7591
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: ScreenGrab

Post by infratec »

Pud
User
User
Posts: 29
Joined: Mon Dec 28, 2009 2:23 am

Re: ScreenGrab

Post by Pud »

I’m sure I looked at these.
The screen capture bit is easy enough provided it’s a whole screen or window or you provide the x y w d cords. I needed to select and pick part of a website image

It’s the picking of the region that was initially a bit more awkward and I can’t see any of the examples that do that. I’d be please to be corrected.
User avatar
OldSkoolGamer
Enthusiast
Enthusiast
Posts: 150
Joined: Mon Dec 15, 2008 11:15 pm
Location: Nashville, TN
Contact:

Re: ScreenGrab(Windows only)

Post by OldSkoolGamer »

Pretty good, but if you drag from right to left it doesn't capture anything due to negative values calculated for the capture box.
Post Reply