Image not grabbed/copied

Just starting out? Need help? Post your questions and find answers here.
User avatar
Lord
Addict
Addict
Posts: 847
Joined: Tue May 26, 2009 2:11 pm

Image not grabbed/copied

Post by Lord »

Hi!

I tried to use a code snipped by idle:
viewtopic.php?f=13&t=70444&start=28

Code: Select all

EnableExplicit

Enumeration
  #Win1
  #Win2
EndEnumeration
Enumeration
  #ImgG1
  #ImgG2
EndEnumeration
Enumeration
  #cImg1
  #cImg2
EndEnumeration
#CAPTUREBLT = $40000000 

Define Event, Quit

Procedure CapWindow(Win)
  ; Screenshot by idle
  ; https://www.purebasic.fr/english/viewtopic.php?f=13&t=70444&start=28
  Protected Rect.rect 
  Protected SrcDc, DestDc, Result, W, H, hwnd
  Debug "Enter CapWindow("+Win+")"
  hwnd=WindowID(Win):Debug "hwnd="+hwnd
  If hwnd 
    SrcDc = GetDC_(0):Debug "SrcDC="+SrcDc
    If SrcDc 
      GetWindowRect_(Hwnd,@Rect)
      W = Rect\Right-Rect\Left :Debug "W="+w
      H = Rect\Bottom - Rect\Top :Debug "H="+h
      If W And H
        CreateImage(#cImg1,W,H, 32)
        If IsImage(#cImg1) :Debug "#Img1 created"
          DestDc = StartDrawing(ImageOutput(#cImg1)):Debug "DestDC="+DestDc
          If DestDc 
            Result = BitBlt_(DestDc,0,0,W,H,Srcdc,Rect\Left,Rect\Top,#SRCCOPY | #CAPTUREBLT):Debug "Result="+Result 
            If Result
              Debug "GrabImage="+GrabImage(#cImg1, #cImg2, 0, 0, W, H); fails
;               Debug "CopyImage="+CopyImage(#cImg1, #cImg2); fails too
              ProcedureReturn IsImage(#cImg2)
            EndIf 
          StopDrawing()
          EndIf 
        EndIf   
      EndIf   
    EndIf
  EndIf  
EndProcedure
Procedure Key()
  Debug "Enter Key()"
  If CapWindow(#Win1)
    SetGadgetState(#ImgG2, ImageID(#cImg2))
    SetClipboardImage(#cImg2)
    ShowLibraryViewer("Image",#ImgG2)
  EndIf
EndProcedure
Procedure MakeImg1()
  CreateImage(#cImg1, 640, 480, 32)
  StartDrawing(ImageOutput(#cImg1))
    Circle(320, 240, 100, $0000FF)
  StopDrawing()
  SetGadgetState(#Win1, ImageID(#cImg1))
EndProcedure

ClearClipboard()
OpenWindow(#Win1, 10, 10, 640, 480, "W1")
ImageGadget(#ImgG1, 0, 0, WindowWidth(#Win1), WindowHeight(#Win1), 0)
OpenWindow(#Win2, 640, 480, 640, 480, "W2")
ImageGadget(#ImgG2, 0, 0, WindowWidth(#Win2), WindowHeight(#Win2), 0)
SetActiveWindow(#Win1)

AddKeyboardShortcut(#Win1, #PB_Shortcut_Alt|#PB_Shortcut_P, 1)
BindEvent(#PB_Event_Menu, @Key(), #Win1)
MakeImg1()


Repeat
  Event=WaitWindowEvent()
  Select Event
    Case #PB_Event_CloseWindow
      Quit=#True
  EndSelect
Until Quit
When using ALT P the image #cImg1 is not copied/grabbed to #cImg2.
#cImg1 is present and valid as seen with ShowLibrary() but #cImg2 is not.
I always got an empty image as #cImg2 back.

What is going on here?
Image
User avatar
ChrisR
Addict
Addict
Posts: 1127
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Image not grabbed/copied

Post by ChrisR »

Your GrabImage must be done after StopDrawing()

Code: Select all

DestDc = StartDrawing(ImageOutput(#cImg1)):Debug "DestDC="+DestDc
If DestDc 
  Result = BitBlt_(DestDc,0,0,W,H,Srcdc,Rect\Left,Rect\Top,#SRCCOPY | #CAPTUREBLT):Debug "Result="+Result 
  StopDrawing()
EndIf 
If Result
  Debug "GrabImage="+GrabImage(#cImg1, #cImg2, 0, 0, W, H)
  ProcedureReturn IsImage(#cImg2)
EndIf
User avatar
Lord
Addict
Addict
Posts: 847
Joined: Tue May 26, 2009 2:11 pm

Re: Image not grabbed/copied

Post by Lord »

ChrisR wrote: Thu Jun 03, 2021 10:54 am Your GrabImage must be done after StopDrawing()
...
OMG! It's that easy. Shame on me.
Thanks for pointing this out.
ChrisR wrote: Thu Jun 03, 2021 10:54 am ]...
I don't know if I'm alone, but the code displayed using [ code-pb ] [ /code-pb ] tags is blurred, as in Lord's post.
If mouse is not over "PureBasic" or "copy" the text is greyed.
If mouse points to one of these, its becoming readable black.
But "copy" is partly hoovering over scrollbar button.
Image
Post Reply