QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: From Ariel on March 08, 2019, 05:42:57 am

Title: How to copy a portion of the screen and save it to a bitmap or PNG file in QB64?
Post by: From Ariel on March 08, 2019, 05:42:57 am
I need to copy a specific portion of the screen and save it to a bitmap (BMP) or PNG file after drawing some stuff to that section of the screen.

I'm having trouble understanding the new _PUTIMAGE vs GET and PUT. I'm not sure how to do this. :(
Title: Re: How to copy a portion of the screen and save it to a bitmap or PNG file in QB64?
Post by: SMcNeill on March 08, 2019, 05:49:04 am
https://www.qb64.org/forum/index.php?topic=47.msg102649#msg102649

My SaveImage Library is what you’re looking for.  I offered it to be added to the toolkit forum, but it hasn’t been added yet, for whatever reason, so it’d be easy for people to find and make use of.
Title: Re: How to copy a portion of the screen and save it to a bitmap or PNG file in QB64?
Post by: From Ariel on March 08, 2019, 06:17:12 am
https://www.qb64.org/forum/index.php?topic=47.msg102649#msg102649

My SaveImage Library is what you’re looking for.  I offered it to be added to the toolkit forum, but it hasn’t been added yet, for whatever reason, so it’d be easy for people to find and make use of.

Thanks I will give this a try!
Title: Re: How to copy a portion of the screen and save it to a bitmap or PNG file in QB64?
Post by: From Ariel on March 08, 2019, 02:53:28 pm
https://www.qb64.org/forum/index.php?topic=47.msg102649#msg102649

My SaveImage Library is what you’re looking for.  I offered it to be added to the toolkit forum, but it hasn’t been added yet, for whatever reason, so it’d be easy for people to find and make use of.

SMcNeill, sadly this doesn't appear to work with InForm. I get an image but it is blank except for the color of the background of the form created by InForm.

By default it will give me an image of the entire program window. If I enclose it in BeginDraw it only copies the section of the pictureBox I specified but again nothing that was drawn onto it.

Code: QB64: [Select]
  1. BeginDraw DrawScr ' DrawScr is the name of the pictureBox I am drawing to
  2.  
  3.             _SOURCE Control(DrawScr).HelperCanvas ' Fellippe Heitor Said this might help it to target the content but it doesn't seem to work.
  4.  
  5.             exportimage1$ = "testimage.png"
  6.             Result = SaveImage(exportimage1$, 0, 0, 0, _WIDTH, _HEIGHT)
  7.             IF Result = 1 THEN 'file already found on drive
  8.                 KILL exportimage1$ 'delete the old file
  9.                 Result = SaveImage(exportimage1$, 0, 0, 0, _WIDTH, _HEIGHT) 'save the new one again
  10.             END IF
  11.             'PRINT Result
  12.             'PRINT "Our initial Image"
  13.             'IF Result < 0 THEN PRINT "Successful PNG export" ELSE PRINT "PNG Export failed."; Result: ' END
  14.            
  15.            
  16. EndDraw DrawScr
  17.  

Title: Re: How to copy a portion of the screen and save it to a bitmap or PNG file in QB64?
Post by: bplus on March 09, 2019, 12:47:37 pm
I have been meaning to check Steve's SaveImage code for some time. Today I finally got around to it.

Here I display the image to grab an image section from the mouse down at top, left corner of section I want and release mouse in corner to right and down from the mouse down. It works.

Perhaps some practice with code before trying it with InForm might be of service:
  [ This attachment cannot be displayed inline in 'Print Page' view ]  

Title: Re: How to copy a portion of the screen and save it to a bitmap or PNG file in QB64?
Post by: FellippeHeitor on March 09, 2019, 04:59:50 pm
@From Ariel:

I checked how Steve's library works and to have it work with InForm you won't actually need the _SOURCE call I imagined initially. You'll need to pass it the actual handle of the PictureBox's helper canvas. In your sample code above, make the following change:

Code: QB64: [Select]
  1. 'No need for BeginDraw DrawScr
  2.  
  3.             'No need for _SOURCE Control(DrawScr).HelperCanvas
  4.  
  5.             exportimage1$ = "testimage.png"
  6.             Result = SaveImage(exportimage1$, Control(DrawScr).HelperCanvas, 0, 0, _WIDTH(Control(DrawScr).HelperCanvas), _HEIGHT(Control(DrawScr).HelperCanvas)) '<-- this is the important change
  7.             IF Result = 1 THEN 'file already found on drive
  8.                 KILL exportimage1$ 'delete the old file
  9.                 Result = SaveImage(exportimage1$, Control(DrawScr).HelperCanvas, 0, 0, _WIDTH(Control(DrawScr).HelperCanvas), _HEIGHT(Control(DrawScr).HelperCanvas)) '<-- same here
  10.             END IF
  11.  
  12.            
  13.            
  14. 'Again, no need for EndDraw DrawScr
Title: Re: How to copy a portion of the screen and save it to a bitmap or PNG file in QB64?
Post by: From Ariel on March 09, 2019, 05:19:14 pm
@From Ariel:

I checked how Steve's library works and to have it work with InForm you won't actually need the _SOURCE call I imagined initially. You'll need to pass it the actual handle of the PictureBox's helper canvas. In your sample code above, make the following change:

Code: QB64: [Select]
  1. 'No need for BeginDraw DrawScr
  2.  
  3.             'No need for _SOURCE Control(DrawScr).HelperCanvas
  4.  
  5.             exportimage1$ = "testimage.png"
  6.             Result = SaveImage(exportimage1$, Control(DrawScr).HelperCanvas, 0, 0, _WIDTH(Control(DrawScr).HelperCanvas), _HEIGHT(Control(DrawScr).HelperCanvas)) '<-- this is the important change
  7.             IF Result = 1 THEN 'file already found on drive
  8.                 KILL exportimage1$ 'delete the old file
  9.                 Result = SaveImage(exportimage1$, Control(DrawScr).HelperCanvas, 0, 0, _WIDTH(Control(DrawScr).HelperCanvas), _HEIGHT(Control(DrawScr).HelperCanvas)) '<-- same here
  10.             END IF
  11.  
  12.            
  13.            
  14. 'Again, no need for EndDraw DrawScr

That did it but I had to enclose the code inside of the BeginDraw and EndDraw or it didn't even generate an image at all. This may have something to do with the picture box being inside a form vs on the root object IDK. But It works!!!

  [ This attachment cannot be displayed inline in 'Print Page' view ]