It is currently Thu Apr 02, 2020 7:31 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: CanvasGadget Catch or Grab
PostPosted: Thu Feb 06, 2020 10:38 am 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 2332
Location: Germany
Maybe somebody could use it...

To save image, copy to another gadget, etc

Update Example
- Add Save

Update v1.03
- Check StartDrawing

Code:
;-TOP

; Comment : CanvasGadget Catch or Grab
; Author  : mk-soft
; Version : v1.03.0
; Create  : 06.02.2020

Procedure CatchCanvas(Gadget, Image = #PB_Any, Depth = 32, Scale = #False)
  Protected r1, id, dx, dy
  If Scale
    dx = DesktopScaledX(GadgetWidth(Gadget))
    dy = DesktopScaledY(GadgetHeight(Gadget))
  Else
    dx = GadgetWidth(Gadget)
    dy = GadgetHeight(Gadget)
  EndIf
  r1 = CreateImage(Image, dx, dy, Depth)
  If r1
    If Image = #PB_Any
      id = r1
    Else 
      id = Image
    EndIf
    If StartDrawing(ImageOutput(id))
      DrawImage(GetGadgetAttribute(Gadget, #PB_Canvas_Image), 0, 0, dx, dy)
      StopDrawing()
    Else
      FreeImage(r1)
      r1 = 0
    EndIf
  EndIf
  ProcedureReturn r1
EndProcedure

Procedure GrabCanvas(Gadget, Image, x, y, Width, Height, Depth = 32, Scale = #False)
  Protected r1, id, dx, dy
  If Scale
    x = DesktopScaledX(x)
    y = DesktopScaledX(y)
    Width = DesktopScaledX(Width)
    Height = DesktopScaledY(Height)
  EndIf
  r1 = CreateImage(Image, Width, Height, Depth)
  If r1
    If Image = #PB_Any
      id = r1
    Else 
      id = Image
    EndIf
    If StartDrawing(ImageOutput(id))
      DrawImage(GetGadgetAttribute(Gadget, #PB_Canvas_Image), 0 - x, 0 - y)
      StopDrawing()
    Else
      FreeImage(r1)
      r1 = 0
    EndIf
  EndIf
  ProcedureReturn r1
EndProcedure

; ****

;- Example

CompilerIf #PB_Compiler_IsMainFile
 
  Enumeration Windows
    #Main
  EndEnumeration
 
  Enumeration Gadgets
    #Canvas
    #Button_Catch
    #Button_Grab
    #Button_Save
  EndEnumeration
 
  Enumeration Status
    #MainStatusBar
  EndEnumeration
 
  Enumeration Images
    #Image_Canvas
  EndEnumeration
 
  Procedure DrawCanvas(Gadget)
    Protected dx, dy
    If StartDrawing(CanvasOutput(Gadget))
      dx = GadgetWidth(Gadget)
      dy = GadgetHeight(Gadget)
      Box(0, 0, dx, dy, $8B8B00)
      DrawingMode(#PB_2DDrawing_Outlined)
      Box(10 + 1, 10 + 1, dx - 20, dy - 20, #White)
      Box(10, 10, dx - 20, dy - 20, #Black)
      DrawingMode(#PB_2DDrawing_Default)
      DrawText(25, 25, " < I like Purebasic > ", #White, #Blue)
      StopDrawing()
    EndIf
   
  EndProcedure
 
  Procedure SaveCanvasImage(Gadget)
    Protected r1, file.s, image
    file = SaveFileRequester("Save Image", "Image.bmp", "", 0)
    If file
      image = CatchCanvas(Gadget, #PB_Any)
      If image
        r1 = SaveImage(image, file)
        FreeImage(image)
      EndIf
    EndIf
    ProcedureReturn r1
  EndProcedure
 
  Procedure Main()
   
    If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, 600, 400, "Window" , #PB_Window_SystemMenu)
      CanvasGadget(#Canvas, 10, 10, 580, 380, #PB_Canvas_Container)
      ButtonGadget(#Button_Catch, 20, 335, 120, 25, "Catch")
      ButtonGadget(#Button_Grab, 150, 335, 120, 25, "Grab")
      ButtonGadget(#Button_Save, 280, 335, 120, 25, "Save")
      DrawCanvas(#Canvas)
     
      Repeat
        Select WaitWindowEvent()
          Case #PB_Event_CloseWindow
            Break
          Case #PB_Event_Gadget
            Select EventGadget()
              Case #Button_Catch
                If CatchCanvas(#Canvas, #Image_Canvas)
                  ShowLibraryViewer("Image", #Image_Canvas)
                EndIf
              Case #Button_Grab
                If GrabCanvas(#Canvas, #Image_Canvas, 15, 15, 200, 40, 24)
                  ShowLibraryViewer("Image", #Image_Canvas)
                EndIf
              Case #Button_Save
                SaveCanvasImage(#Canvas)
               
            EndSelect
           
        EndSelect
      ForEver
     
    EndIf
   
  EndProcedure : Main()
 
CompilerEndIf

_________________
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Last edited by mk-soft on Thu Feb 06, 2020 1:59 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: CanvasGadget Catch or Grab
PostPosted: Thu Feb 06, 2020 12:26 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Sep 11, 2016 2:17 pm
Posts: 638
Neat idea :)
You forgot errorhandling for StartDrawing().


Top
 Profile  
Reply with quote  
 Post subject: Re: CanvasGadget Catch or Grab
PostPosted: Thu Feb 06, 2020 2:00 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 2332
Location: Germany
Update v1.03
- Check StartDrawing

Ok :wink:

_________________
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
 Post subject: Re: CanvasGadget Catch or Grab
PostPosted: Thu Feb 06, 2020 6:35 pm 
Offline
User
User

Joined: Fri Dec 06, 2013 11:57 am
Posts: 35
Nice tool,thanks. Actually having someproblems with Canvas, Grab und Draw on Mac, so will give it a try.


Top
 Profile  
Reply with quote  
 Post subject: Re: CanvasGadget Catch or Grab
PostPosted: Fri Feb 07, 2020 4:57 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Nov 05, 2006 11:42 pm
Posts: 4658
Location: Lyon - France
Thanks for sharing 8)

_________________
ImageThe happiness is a road...
Not a destination


Top
 Profile  
Reply with quote  
 Post subject: Re: CanvasGadget Catch or Grab
PostPosted: Fri Feb 07, 2020 7:37 pm 
Offline
Addict
Addict

Joined: Fri Nov 09, 2012 11:04 pm
Posts: 1750
Location: Uttoxeter, UK
Very nice, thank you.

_________________
DE AA EB


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye