Grabscreen not found

Just starting out? Need help? Post your questions and find answers here.
User avatar
bfernhout
Enthusiast
Enthusiast
Posts: 123
Joined: Mon Feb 26, 2018 10:41 pm
Location: Netherlands
Contact:

Grabscreen not found

Post by bfernhout »

What i want to say is this:
I can grab a whole playing screen when i use to get GrabSprite.
But i can't find a way to grab the whole screen using GrabImage

E.G.

Code: Select all

MyScreen = Grabsprite(#PB_Any,0,0,640,480)
This is working and i can get to display the whole screen again and again as many times i like.

but:

Code: Select all

MyScreen = GrabImage(Outputscreen(),#PB_Any,0,0,640,480)
Give me a error. Whatever i tryed i always get an error. Or the code i use is wrong (What i think it is). Or there is no way to find any solution to this option.

The reason why i ask this is bcause i am converting a game i did made a long time ago. And in that i use to grab the screen i am working on and overlaying a sprite. It is a way op programming i have learnd to forget. Cause its much to fuzzy. And to many assumptions witch is not in place. The game is easy and the conversion is working. But the assumptions i had programmed in is backfirering now. The machines we use today a abau 5 times faster then the machines i did used then. And now the game is running very fast.

Try to keep the game original as possible and adding machine depandence delays in. So the game is working for thw future.
When its done i wil post the whole code so people can see what is was doing when i was younger and where there where mistakes made. So people can learn from this.

Bart Fernhout.
From my first self made computer till now I stil like computers.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Grabscreen not found

Post by infratec »

Hi,

I dont't know OutputScreen() ...
Maybe this is waht you want:

Code: Select all

If StartDrawing(ScreenOutput())
  MyScreen = GrabDrawingImage(#PB_Any, 0, 0, ScreenWidth(), ScreenHeight())
  StopDrawing()
EndIf
Bernd
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: Grabscreen not found

Post by walbus »

@Bernd
I don't think GrabImage works with Screen.
There is the possibility to write on a sprite and then grab the content of the sprite.
In GFX_WIzzard_BF you can use the functions

There is also the possibility to read out the screen directly, but this doesn't make sense to me.
It usually does mostly also not make sense to write directly on the screen.

Code: Select all

CopyContent_PB_Sprite_Image_BF(source_ID, destination_ID) ; Move a content from PB sprite to image - Same size
  
CopyContent_PB_Sprite_Canvas_BF(source_ID, destination_ID) ; Move a content from PB sprite to canvas - Same size
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Grabscreen not found

Post by infratec »

Hi,

just tested:

Code: Select all

;
; ------------------------------------------------------------
;
;   PureBasic - Sprite example file
;
;    (c) Fantaisie Software
;
; ------------------------------------------------------------
;

If InitSprite() = 0 Or InitKeyboard() = 0
  MessageRequester("Error", "Sprite system can't be initialized", 0)
  End
EndIf

;
; Now, open a 800*600 - 32 bits screen
;
If OpenScreen(800, 600, 32, "Sprite")

  ; Load our 16 bit sprite (which is a 24 bit picture in fact, as BMP doesn't support 16 bit format)
  ; 
  LoadSprite(0, #PB_Compiler_Home + "examples/sources/Data/PureBasic.bmp")
  CopySprite(0, 1, 0)
  
  Repeat
    
    ; Inverse the buffers (the back become the front (visible)... And we can do the rendering on the back)
    
    FlipBuffers()
    
    ClearScreen(RGB(0,0,0))
    
    ; Draw our sprite

    ClipSprite(0, 0, 0, x, x/8)
     
    DisplaySprite(0, x, 100)
    DisplaySprite(1, x, x)
    DisplaySprite(0, 600-x, x)
    
    x+1
    
    ExamineKeyboard()
  Until KeyboardPushed(#PB_Key_Escape)
  
  If StartDrawing(ScreenOutput())
    MyScreen = GrabDrawingImage(#PB_Any, 0, 0, ScreenWidth(), ScreenHeight())
    StopDrawing()
  EndIf
  
  SaveImage(MyScreen, "c:\tmp\test.bmp")
  
Else
  MessageRequester("Error", "Can't open a 800*600 - 32 bit screen !", 0)
EndIf
it works.
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: Grabscreen not found

Post by walbus »

I am not sure, it was for a longer time

I have test it now, it works on all OS

Thanks for your hint and code,
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Grabscreen not found

Post by infratec »

For the lazy ones:

Code: Select all

Procedure.i GrabScreen(x.i=0, y.i=0, width.i=0, height.i=0)
  
  Protected Result.i
  
  
  If StartDrawing(ScreenOutput())
    If Not width
      width = ScreenWidth()
    EndIf
    If Not height
      height = ScreenHeight()
    EndIf
    
    Result = GrabDrawingImage(#PB_Any, x, y, width, height)
    
    StopDrawing()
  EndIf
  
  ProcedureReturn Result
  
EndProcedure
Bernd
User avatar
bfernhout
Enthusiast
Enthusiast
Posts: 123
Joined: Mon Feb 26, 2018 10:41 pm
Location: Netherlands
Contact:

Re: Grabscreen not found

Post by bfernhout »

Sorry for the late reply.
Sometimes i am not at home. And the work i do is at that moment more important then looking on this forum.

But anyway, Many thanks for the solution. I used it and it work. Sometimes i think why not write it in the manual. Its a small paragraf in the text.

Thank you all.
From my first self made computer till now I stil like computers.
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: Grabscreen not found

Post by walbus »

Hi
The creation of a comprehensive and almost complete and
perfect manual is extremely difficult and time-consuming
The PB reference manual is a very good work
It is also constantly maintained and updated

Regards Werner
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Grabscreen not found

Post by infratec »

Hi, hi,

it is written in the help.
Else ... from where should I know it?
User avatar
Blue
Addict
Addict
Posts: 864
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Grabscreen not found

Post by Blue »

infratec wrote:For the lazy ones:

Code: Select all

Procedure.i GrabScreen(x.i=0, y.i=0, width.i=0, height.i=0)
  ...
  Result = GrabDrawingImage(#PB_Any, x, y, width, height)
  ...
EndProcedure
Bernd
Genial code, and exquisite timing !
I was just about to post 2 questions, asking
(1) what purpose does GrabDrawingImage() serve in life ?
(2) how can you grab a small piece of the window display ?
when a quick search brought me this offering from Bernd to lazy programmers.
Perfect.
I just replaced ScreenOutput() with WindowOuput(), and voilà.
Thank you Bernd.
"That's not a bug..." said the programmer. "it's a feature! "
"Oh! I see..." replied the blind man.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Grabscreen not found

Post by infratec »

For the really lazy ones

Code: Select all

Procedure.i GrabWindow(Window.i, x.i=0, y.i=0, width.i=0, height.i=0)
 
  Protected Result.i
 
 
  If StartDrawing(WindowOutput(Window))
    If Not width
      width = WindowWidth(Window)
    EndIf
    If Not height
      height = WindowHeight(Window)
    EndIf
   
    Result = GrabDrawingImage(#PB_Any, x, y, width, height)
   
    StopDrawing()
  EndIf
 
  ProcedureReturn Result
 
EndProcedure
:mrgreen:
User avatar
Bisonte
Addict
Addict
Posts: 1226
Joined: Tue Oct 09, 2007 2:15 am

Re: Grabscreen not found

Post by Bisonte »

And now the laziest :mrgreen:

Code: Select all

Enumeration
  #PB_Window
  #PB_Canvas
  #PB_Image
  #PB_Screen
EndEnumeration

Procedure.i LazyGrab(Type, Object, x = 0, y = 0, width = 0, height = 0)
 
  Protected dc.i, Result.i
 
  Select Type
      
    Case #PB_Window
      If IsWindow(Object)
        If Not Width  : Width = WindowWidth(Object)   : EndIf
        If Not Height : Height = WindowHeight(Object) : EndIf
        dc = StartDrawing(WindowOutput(Object))
      EndIf
      
    Case #PB_Image
      If IsImage(Object)
        If Not Width  : Width  = ImageWidth(Object)  : EndIf
        If Not Height : Height = ImageHeight(Object) : EndIf
        dc = StartDrawing(ImageOutput(Object))
      EndIf
      
    Case #PB_Canvas
      If IsGadget(Object)
        If GadgetType(Object) = #PB_GadgetType_Canvas
        If Not Width  : Width  = GadgetWidth(Object)  : EndIf
        If Not Height : Height = GadgetHeight(Object) : EndIf
        dc = StartDrawing(CanvasOutput(Object))
      EndIf
            
    Case #PB_Screen
      If IsScreenActive()
        If Not Width  : Width = ScreenWidth()   : EndIf
        If Not Height : Height = ScreenHeight() : EndIf
        dc = StartDrawing(ScreenOutput())
      EndIf
           
  EndSelect
  
  If dc
    Result = GrabDrawingImage(#PB_Any, x, y, width, height)
    StopDrawing()
  EndIf
 
  ProcedureReturn Result
 
EndProcedure
PureBasic 6.04 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: Grabscreen not found

Post by walbus »

For the laziest, you can check all object types automatically before the select call

Then all works automatically, so you must not pre define the object type :wink:

I don't think for Mac the output goes to the window
An output direct to the window is also extremely slow and should always be avoided

Then it is very practical and does the work for the user

Its available GrabDrawingImage works to time not ever correctly, but test self on PB562, i have not check it again :|
I had problems with GrabDrawingImage, so i have it use not again
http://www.purebasic.fr/english/viewtop ... 24&t=69401
http://www.purebasic.fr/english/viewtop ... =4&t=69095
User avatar
Blue
Addict
Addict
Posts: 864
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Grabscreen not found

Post by Blue »

Bernd started a trend : solutions for lazy programmers obviously gets the creative juices flowing for a number of PB enthusiasts.

But, to all of you, I ask:
Why not

Code: Select all

 ...
    If  Width < 1 :  Width = OutputWidth()  : EndIf
    If Height < 1 : Height = OutputHeight() : EndIf
 ...
instead of the output-specific function you call up in each possible case ?
It seems to me that would streamline the code.
Are OutputWidth()/Height() not reliable ?
"That's not a bug..." said the programmer. "it's a feature! "
"Oh! I see..." replied the blind man.
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: Grabscreen not found

Post by walbus »

Hi blue
I have created a lot multi use functions
Primary this make many things very simple

Look as sample

Code: Select all

CopyContent_BF(source_ID, destination_ID, output_x=0, output_y=0) ; Move a content from image to image
                                                                            ; Canvas to canvas, canvas to image, image to canvas, PB sprite to canvas
                                                                            ; Canvas to PB sprite, PB sprite to image, image to PB sprite (same sized)
                                                                            ; For output on screen set ActivateScreenOutput_BF(1) - then a destination_ID is not needed
The Grab functions above in this thread have maked me many trouble, so, mostly i use this functions to time not

For my is urgent : I will make all functions bullet proof, with the simplest handling

Mostly from my functions can output at the same on all available destinations, for screen output you can set simple a flag

Look, this is a typical selection part

Code: Select all

    If IsImage(input_ID) ; check for image
      image=1
    ElseIf IsSprite(input_ID) ; Check for PB sprite
      sprite=1
    ElseIf Not IsGadget(input_ID) ; Check for canvas
      ProcedureReturn -17
    EndIf
    
    If image
      output_width=ImageWidth(input_ID)-1
      output_height=ImageHeight(input_ID)-1
    ElseIf sprite
      sprite_w_h(input_ID, @sprite_w_h)
      output_width=sprite_w_h\sprite_w-1
      output_height=sprite_w_h\sprite_h-1
    Else
      output_width=GadgetWidth(input_ID)-1
      output_height=GadgetHeight(input_ID)-1
    EndIf
    
    If x<0 : x=0 : EndIf
    If y<0 : y=0 : EndIf
    If x>output_width : x=output_width : EndIf
    If y>output_height : y=output_height : EndIf
    If xx>output_width : xx=output_width : EndIf
    If yy>output_height : yy=output_height : EndIf
    If x>xx : x=xx : EndIf
    If y>yy : y=yy : EndIf
    
    If image
      result=StartDrawing(ImageOutput(input_ID))
    ElseIf sprite
      result=StartDrawing(SpriteOutput(input_ID))
    Else
      result=StartDrawing(CanvasOutput(input_ID))
    EndIf
Or so

Code: Select all

If screen_output
      mode=1
      image_width_1=ScreenWidth()
      image_height_1=ScreenHeight()
    Else
      If IsImage(output_ID) ; check for image
        image=1
        image_width_1=ImageWidth(output_ID)
        image_height_1=ImageHeight(output_ID)
      ElseIf IsSprite(output_ID) ; Check for PB sprite
        sprite=1
        sprite_w_h(output_ID, @sprite_w_h)
        image_width_1=sprite_w_h\sprite_w
        image_height_1=sprite_w_h\sprite_h
      ElseIf IsGadget(output_ID) ; Check for canvas
        image_width_1=GadgetWidth(output_ID)
        image_height_1=GadgetHeight(output_ID)
      Else
        ProcedureReturn -11
      EndIf
    EndIf
    
    If mode<>-1
      If IsImage(texture_ID)
        texture_width_=ImageWidth(texture_ID)-1
        texture_height_=ImageHeight(texture_ID)-1
      Else
        If IsSprite(texture_ID)
          sprite_input=1
          sprite_w_h(texture_ID, @sprite_w_h)
          texture_width_=sprite_w_h\sprite_w-1
          texture_height_=sprite_w_h\sprite_h-1
        ElseIf IsGadget(texture_ID)
          canvas_input=1
          texture_width_=GadgetWidth(texture_ID)-1
          texture_height_=GadgetHeight(texture_ID)-1
        Else
          ProcedureReturn -17    
        EndIf
      EndIf
    EndIf
    
    If texture_width>image_width_1-texture_x Or texture_width<1
      texture_width=image_width_1-texture_x
    EndIf
    If texture_height>image_height_1-texture_y Or texture_height<1
      texture_height=image_height_1-texture_y
    EndIf
    
    image_width=image_width_1-1
    image_height=image_height_1-1
A output on a Window is never a good idea, so i support this not
Its also never needed, a better way is ever available

Regards Werner
Post Reply