Author Topic: How to copy a portion of the screen and save it to a bitmap or PNG file in QB64?  (Read 3495 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline From Ariel

  • Newbie
  • Posts: 4
  • Meep! Mew! Mer!
    • View Profile
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. :(

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
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.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline From Ariel

  • Newbie
  • Posts: 4
  • Meep! Mew! Mer!
    • View Profile
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!

Offline From Ariel

  • Newbie
  • Posts: 4
  • Meep! Mew! Mer!
    • View Profile
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.  


Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
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:
  [ You are not allowed to view this attachment ]  

« Last Edit: March 09, 2019, 12:49:04 pm by bplus »

Marked as best answer by From Ariel on March 09, 2019, 01:46:56 pm

FellippeHeitor

  • Guest
@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
« Last Edit: March 09, 2019, 05:02:36 pm by FellippeHeitor »

Offline From Ariel

  • Newbie
  • Posts: 4
  • Meep! Mew! Mer!
    • View Profile
@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!!!

  [ You are not allowed to view this attachment ]  
« Last Edit: March 09, 2019, 06:31:47 pm by From Ariel »