Page 1 of 1

Save screen section

Posted: Thu Apr 20, 2023 4:37 pm
by epog10
I have a ContainerGadget with a series of text boxes that get filled in by the program.

What I want to do is to save the ContainerGadget with all the data as an image which will be incorporated into a web page.

Is there a simple way to do that? Obviously I could do a Windows screen capture, then crop the image to the container size and re-save but it would be so much easier for the user of the program if they just pressed a button and it happened.

Of course it need not be a ContainerGadget. I did play with CanvasGadget using the #PB_Canvas_Container flag but I just can't make it work. When I come to SaveImage, it tells me the image is not initialised.

Guess it is just inexperience.

Regards,
epog10

Re: Save screen section

Posted: Thu Apr 20, 2023 5:28 pm
by mk-soft
I searched for something in the forum and adapted it

Org: viewtopic.php?p=539801#p539801

Only Windows:

Update save to downloads

Code: Select all

;-TOP

#ProgramTitle = "Main Window"
#ProgramVersion = "v1.01.2"

UseJPEGImageEncoder()

Enumeration Windows
  #Main
EndEnumeration

Enumeration MenuBar
  #MainMenu
EndEnumeration

Enumeration MenuItems
  #MainMenuAbout
  #MainMenuExit
EndEnumeration

Enumeration Gadgets
  #MainContainer
  #MainEdit
  #MainButton1
  #MainButton2
EndEnumeration

Enumeration StatusBar
  #MainStatusBar
EndEnumeration

Procedure ScreenshotWindow(Window, Image = #PB_Any)
  Protected r1, dx, dy
  
  dx = WindowWidth(Window, #PB_Window_FrameCoordinate)
  dy = WindowHeight(Window, #PB_Window_FrameCoordinate)
  r1 = CreateImage(Image, dx, dy, 24)
  If Image = #PB_Any
    Image = r1
  EndIf
  hDC = StartDrawing(ImageOutput(Image))
  If hDC
    SendMessage_(WindowID(Window), #WM_PRINT, hDC, #PRF_CHILDREN|#PRF_CLIENT|#PRF_NONCLIENT|#PRF_ERASEBKGND)
    StopDrawing()
  EndIf
  ProcedureReturn r1
EndProcedure

Procedure ScreenshotGadget(Gadget, Image = #PB_Any)
  Protected r1, dx, dy
  
  dx = GadgetWidth(Gadget)
  dy = GadgetHeight(Gadget)
  r1 = CreateImage(Image, dx, dy, 24)
  If Image = #PB_Any
    Image = r1
  EndIf
  hDC = StartDrawing(ImageOutput(Image))
  If hDC
    SendMessage_(GadgetID(Gadget), #WM_PRINT, hDC, #PRF_CHILDREN|#PRF_CLIENT|#PRF_NONCLIENT|#PRF_ERASEBKGND)
    StopDrawing()
  EndIf
  ProcedureReturn r1
EndProcedure

Procedure UpdateWindow()
  Protected dx, dy
  dx = WindowWidth(#Main)
  dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
  ; Resize gadgets
  ResizeGadget(#MainContainer, 5, 5, dx - 10, dy - 45)
  ResizeGadget(#MainEdit, 10, 10, GadgetWidth(#MainContainer) - 25, GadgetHeight(#MainContainer) - 25)
  ResizeGadget(#MainButton1, 10, dy - 35, 120, 30)
  ResizeGadget(#MainButton2, dx - 130, dy - 35, 120, 30)
EndProcedure

Procedure Main()
  Protected dx, dy
  
  #MainStyle = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
  
  If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, 800, 600, #ProgramTitle , #MainStyle)
    ; Menu
    CreateMenu(#MainMenu, WindowID(#Main))
    MenuTitle("&File")
    CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
      MenuItem(#PB_Menu_About, "")
    CompilerElse
      MenuItem(#MainMenuAbout, "About")
    CompilerEndIf
    ; Menu File Items
    
    CompilerIf Not #PB_Compiler_OS = #PB_OS_MacOS
      MenuBar()
      MenuItem(#MainMenuExit, "E&xit")
    CompilerEndIf
    
    ; StatusBar
    CreateStatusBar(#MainStatusBar, WindowID(#Main))
    AddStatusBarField(#PB_Ignore)
    
    ; Gadgets
    dx = WindowWidth(#Main)
    dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
    ContainerGadget(#MainContainer, 5, 5, dx - 10, dy - 45, #PB_Container_Double)
    EditorGadget(#MainEdit, 10, 10, GadgetWidth(#MainContainer) - 25, GadgetHeight(#MainContainer) - 25)
    SetGadgetColor(#MainEdit, #PB_Gadget_BackColor, $B5E4FF)
    CloseGadgetList()
    ButtonGadget(#MainButton1, 10, dy - 35, 120, 30, "Window")
    ButtonGadget(#MainButton2, dx - 130, dy - 35, 120, 30, "Gadget")
    
    ; Bind Events
    BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), #Main)
    
    ; Event Loop
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Select EventWindow()
            Case #Main
              Break
              
          EndSelect
          
        Case #PB_Event_Menu
          Select EventMenu()
            CompilerIf #PB_Compiler_OS = #PB_OS_MacOS   
              Case #PB_Menu_About
                PostEvent(#PB_Event_Menu, #Main, #MainMenuAbout)
                
              Case #PB_Menu_Preferences
                
              Case #PB_Menu_Quit
                PostEvent(#PB_Event_CloseWindow, #Main, #Null)
                
            CompilerEndIf
            
          Case #MainMenuAbout
            MessageRequester("About", #ProgramTitle + #LF$ + #ProgramVersion, #PB_MessageRequester_Info)
              
          Case #MainMenuExit
            PostEvent(#PB_Event_CloseWindow, #Main, #Null)
            
          EndSelect
          
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #MainEdit
              Select EventType()
                Case #PB_EventType_Change
                  ;
                  
              EndSelect
              
            Case #MainButton1
              r1 = ScreenshotWindow(#Main, 1)
              ShowLibraryViewer("Image", 1)
              ;SetClipboardImage(1)
              SaveImage(1, GetUserDirectory(#PB_Directory_Downloads) + "Image_Window.jpg", #PB_ImagePlugin_JPEG)
              
            Case #MainButton2
              r2 = ScreenshotGadget(#MainContainer, 2)
              ShowLibraryViewer("Image", 2)
              ;SetClipboardImage(2)
              SaveImage(2, GetUserDirectory(#PB_Directory_Downloads) + "Image_Gadget.jpg", #PB_ImagePlugin_JPEG)
              
          EndSelect
          
      EndSelect
    ForEver
    
  EndIf
  
EndProcedure : Main()

Re: Save screen section

Posted: Tue Apr 25, 2023 2:35 pm
by epog10
Thank you very much for your response.

I am in the process of converting my original progam so that it uses the Gadget save in your code and it is working well.

Regards,
epog10