Author Topic: SaveImage (take screenshots) by SMcNeill  (Read 9114 times)

0 Members and 1 Guest are viewing this topic.

Offline The Librarian

  • Moderator
  • Newbie
  • Posts: 39
    • View Profile
SaveImage (take screenshots) by SMcNeill
« on: August 24, 2019, 05:38:01 pm »
SaveImage

Contributor(s): @SMcNeill
Source: qb64.org Forum
URL: https://www.qb64.org/forum/index.php?topic=2701.0
Tags: [bmp] [export] [gif] [graphics] [jpg] [png] [zlib]

Description:
How to save a QB64 screen image in either BMP or PNG format; how one can easily turn a text screen into a 256 color graphic screen with one simple command; and how to compress and inflate strings or programs quickly and easily.
There's a lot of stuff packaged inside the library here, but the main ones which a user might decide to call is the following:
FUNCTION SaveImage (file$, image&, x1%, y1%, x2%, y2%) 
SUB SaveFullImage (filename$)
SUB SaveFullBMP (filename$)
SUB SaveFullJPG (filename$)
SUB SaveBMP (filename$, image&, x1%, y1%, x2%, y2%)
FUNCTION TextScreenToImage256& (image&)
FUNCTION TextScreenToImage32& (image&)
FUNCTION Deflate$ (text$) 
FUNCTION Inflate$ (text$)
FUNCTION PNGExport (file$, imagehandle%, x1%, y1%, x2%, y2%) 
SUB SaveJPG (file$, image&, startx, starty, finishx, finishy)

As you can see, most of these routines can easily be handled by simply using FUNCTION SaveImage, which then decides if it needs to convert from a text screen to a graphic screen, and which save routine to make use of.


Source Code:
Code: QB64: [Select]
  1. '$INCLUDE:'SaveImage.BI'
  2.  
  3. CONST SaveTextAs256Color = 0 'Flag to Save as 256 color file or 32-bit color file, when converting SCREEN 0 to an image
  4. '                             Set to TRUE (any non-zero value) to save text screens in 256 color mode.
  5. '                             Set to FALSE (zero) to save text screens in 32-bit color mode.
  6.  
  7.  
  8. 'CONST ConvertToStandard256Palette = 0
  9. '                             Set the value to 0 (FALSE) to preseve the color information perfectly, using its default palette.
  10. '                             If the CONST is set (TRUE), then we convert our colors to as close of a match as possible, while
  11. '                             preserving the standard QB64 256-color palette.
  12. '                             Commented here, simply to help folks know that it exists for use when converting a 32 bit image
  13. '                             down to 256 colors, such as what the GIF routine has to do for us (GIFs are limited to 256 color images)
  14.  
  15. SCREEN _NEWIMAGE(1280, 720, 32)
  16. DIM exportimage(4) AS STRING
  17. InitialImage$ = "Volcano Logo.jpg"
  18. exportimage(1) = "testimage.png": exportimage(2) = "testimage.bmp"
  19. exportimage(3) = "testimage.jpg": exportimage(4) = "testimage.gif"
  20.  
  21. 'If you want to test the demo with a screen 0 image, then...
  22. l& = _LOADIMAGE(InitialImage$) 'Remark out this line
  23. '_PUTIMAGE , l& 'And remark the _PUTIMAGE line down below as well
  24.  
  25. 'And unremark the following
  26. 'SCREEN 0
  27. 'FOR i = 0 TO 15
  28. '    COLOR i
  29. '    PRINT "COLOR i"
  30. 'NEXT
  31. 'Then you can watch as we prove that we can save images while in Screen 0 TEXT mode.
  32. FOR i = 1 TO 4
  33.     _PUTIMAGE , l& 'Remark out this line, if you want to see the SCREEN 0 demo
  34.     LOCATE 1, 1
  35.     Result = SaveImage(exportimage(i), 0, 0, 0, _WIDTH - 1, _HEIGHT - 1)
  36.     IF Result = 1 THEN 'file already found on drive
  37.         KILL exportimage(i) 'delete the old file
  38.         Result = SaveImage(exportimage(i), 0, 0, 0, _WIDTH - 1, _HEIGHT - 1) 'save the new one again
  39.     END IF
  40.     PRINT Result
  41.     IF Result < 0 THEN PRINT "Successful " + exportimage(i) + " export" ELSE PRINT ext$ + " Export failed with Error Code:"; Result: ' END
  42.     SLEEP
  43.  
  44. FOR i = 1 TO 4
  45.     zz& = _LOADIMAGE(exportimage(i), 32)
  46.     IF zz& <> -1 THEN
  47.         SCREEN zz&
  48.         PRINT "Image Handle: "; zz&, exportimage(i)
  49.         PRINT "Successful Import using _LOADIMAGE"
  50.     ELSE
  51.         PRINT "ERROR - Not Loading the new image (" + exportimage(i) + ") with _LOADIMAGE."
  52.     END IF
  53.     SLEEP
  54.  
  55.  
  56. '$INCLUDE:'SaveImage.BM'
  57.  

Volcano Logo.jpg

Check out the latest update at the author's Project File in the URL above.
 
 
« Last Edit: July 24, 2021, 06:58:26 am by Junior Librarian »