Author Topic: Simple BMP Save Example  (Read 3863 times)

0 Members and 1 Guest are viewing this topic.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Simple BMP Save Example
« on: December 21, 2019, 12:24:14 am »
If anyone is wondering how to save anything on the screen to a .BMP picture, you don't need any libraries or extra files. Here's an example I've been using for awhile. This isn't a complete program so you will have to add your own code to make a picture on the screen to save it. It's easy to see on this example where to add that. This is just an example to feel free to change anything. The SUB and some other code is from the QB64 wiki pages.

Code: QB64: [Select]
  1. 'Example code of how to save a screen picture to a .bmp file.
  2. start:
  3. picture& = _NEWIMAGE(800, 600, 32)  
  4. SCREEN picture&  
  5. '------------------------------------
  6. 'Make picture here.
  7.  
  8.  
  9.  
  10.  
  11. '------------------------------------
  12. choice:
  13. IF a$="s" OR a$="S" THEN GOTO saving:
  14. IF a$=chr$(27) THEN END
  15. GOTO choice: 'Or you can loop it somewhere if you want the picture to be ongoing.
  16.  
  17. saving:
  18. picture2& = _COPYIMAGE(picture&)
  19. PRINT "                       Saving"
  20. PRINT "         Your bmp file will be saved in the"
  21. PRINT "         same directory as this program is."
  22. PRINT "         It can be used with almost any"
  23. PRINT "         other graphics program or website."
  24. PRINT "         It is saved using:"
  25. PRINT "         width: 800  height: 600 pixels."
  26. PRINT "         Type a name to save your picture"
  27. PRINT "         and press the Enter key. Do not"
  28. PRINT "         add .bmp at the end, the program"
  29. PRINT "         will do it automatically."
  30. PRINT "         Example: MyPic"
  31. PRINT "         Quit and Enter key ends program."
  32. INPUT "         ->"; nm$
  33. IF nm$ = "Quit" OR nm$ = "quit" OR nm$ = "QUIT" THEN END
  34. nm$ = nm$ + ".bmp"
  35. 'Checking to see if the file already exists on your computer.
  36. theFileExists = _FILEEXISTS(nm$)
  37. IF theFileExists = -1 THEN
  38.     PRINT
  39.     PRINT "       File Already Exists"
  40.     PRINT "       Saving will delete your old"
  41.     PRINT "       bmp picture."
  42.     PRINT "       Would you like to still do it?"
  43.     PRINT "      (Y/N)."
  44.     PRINT "      Esc goes to start screen."
  45.     llloop:
  46.     _LIMIT 100
  47.     ag2$ = INKEY$
  48.     IF ag2$ = CHR$(27) THEN GOTO start:
  49.     IF ag2$ = "" THEN GOTO llloop:
  50.     IF ag2$ = "y" OR ag$ = "Y" THEN
  51.         SHELL _HIDE "DEL " + nm$
  52.         GOTO saving2:
  53.     END IF
  54.     GOTO llloop:
  55. saving2:
  56. SCREEN picture&
  57. _PUTIMAGE , picture&
  58. SaveImage 0, nm$
  59. nm$ = ""
  60. FOR snd = 100 TO 700 STEP 100
  61.     SOUND snd, 2
  62. NEXT snd
  63. picture& = 0
  64. picture2& = 0
  65. GOTO start:
  66.  
  67. 'Here is the SUB needed to save the image to BMP.
  68. SUB SaveImage (image AS LONG, filename AS STRING)
  69.     bytesperpixel& = _PIXELSIZE(image&)
  70.     IF bytesperpixel& = 0 THEN PRINT "Text modes unsupported!": END
  71.     IF bytesperpixel& = 1 THEN bpp& = 8 ELSE bpp& = 24
  72.     x& = _WIDTH(image&)
  73.     y& = _HEIGHT(image&)
  74.     b$ = "BM????QB64????" + MKL$(40) + MKL$(x&) + MKL$(y&) + MKI$(1) + MKI$(bpp&) + MKL$(0) + "????" + STRING$(16, 0) 'partial BMP header info(???? to be filled later)
  75.     IF bytesperpixel& = 1 THEN
  76.         FOR c& = 0 TO 255 ' read BGR color settings from JPG image + 1 byte spacer(CHR$(0))
  77.             cv& = _PALETTECOLOR(c&, image&) ' color attribute to read.
  78.             b$ = b$ + CHR$(_BLUE32(cv&)) + CHR$(_GREEN32(cv&)) + CHR$(_RED32(cv&)) + CHR$(0) 'spacer byte
  79.         NEXT
  80.     END IF
  81.     MID$(b$, 11, 4) = MKL$(LEN(b$)) ' image pixel data offset(BMP header)
  82.     lastsource& = _SOURCE
  83.     _SOURCE image&
  84.     IF ((x& * 3) MOD 4) THEN padder$ = STRING$(4 - ((x& * 3) MOD 4), 0)
  85.     FOR py& = y& - 1 TO 0 STEP -1 ' read JPG image pixel color data
  86.         r$ = ""
  87.         FOR px& = 0 TO x& - 1
  88.             c& = POINT(px&, py&) 'POINT 32 bit values are large LONG values
  89.             IF bytesperpixel& = 1 THEN r$ = r$ + CHR$(c&) ELSE r$ = r$ + LEFT$(MKL$(c&), 3)
  90.         NEXT px&
  91.         d$ = d$ + r$ + padder$
  92.     NEXT py&
  93.     _SOURCE lastsource&
  94.     MID$(b$, 35, 4) = MKL$(LEN(d$)) ' image size(BMP header)
  95.     b$ = b$ + d$ ' total file data bytes to create file
  96.     MID$(b$, 3, 4) = MKL$(LEN(b$)) ' size of data file(BMP header)
  97.     IF LCASE$(RIGHT$(filename$, 4)) <> ".bmp" THEN ext$ = ".bmp"
  98.     f& = FREEFILE
  99.     OPEN filename$ + ext$ FOR OUTPUT AS #f&: CLOSE #f& ' erases an existing file
  100.     OPEN filename$ + ext$ FOR BINARY AS #f&
  101.     PUT #f&, , b$
  102.     CLOSE #f&
  103.  
« Last Edit: December 21, 2019, 12:25:30 am by SierraKen »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Simple BMP Save Example
« Reply #1 on: December 21, 2019, 04:32:12 am »
Have you taken a look at the SaveImage library?  https://www.qb64.org/forum/index.php?topic=1651.0

Useage is:

Code: [Select]
‘$INCLUDE:’SaveImage.BI’

result = SaveImageFull (“Filename.BMP”)

‘$INCLUDE:’SaveImage.BM’

Works with all screen modes, including screen 0 text screens.  Saves in BMP, PNG, GIF, and PNG formats just by changing the extension to the desired format.

Other routines in the library allow converting from 32-bit images to 256 color images, saving segments of the screen and not just the whole image, and ZLIB compression/decompression routines.  Most routines are written individually, so you can just extract the code you need for your project, if desired, and ignore all the rest of it.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Simple BMP Save Example
« Reply #2 on: December 21, 2019, 12:18:46 pm »
Steve, I apologize for not commenting on that in the original post. I did in fact look at that post but since I've never used libraries before (except for one time with the windows color picker library which seemed to fail on some people's computers), I haven't looked into using them yet. But I will download it and see what I can do with it sometime. Thanks!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Simple BMP Save Example
« Reply #3 on: December 21, 2019, 01:20:13 pm »
Hi Ken,

The current ColorDialog$ is working for everyone I hope? I just checked your most recent Paint program and have comment about Cancel or Escape from colorDialog in that thread.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Simple BMP Save Example
« Reply #4 on: December 21, 2019, 01:32:07 pm »
Got it, thanks bplus.