User Tools

Site Tools


saving_20a_20png_20image

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

saving_20a_20png_20image [2018/03/31 13:19]
127.0.0.1 external edit
saving_20a_20png_20image [2018/04/13 16:03] (current)
richardrussell Added syntax highlighting
Line 1: Line 1:
 =====Saving a PNG image===== =====Saving a PNG image=====
  
-//by Richard Russell, November 2010//\\ \\  This article describes how to save an image as a PNG file. It relies on the presence of the //GDI Plus// library so will work only on Windows XP (or later) or if you have specifically installed **GDIPLUS.DLL** on the target computer. Microsoft permits you to redistribute this file so you can include it with your program if necessary.\\ \\  To begin with you need to have a **handle** to the bitmap you want to save (see below if instead the bitmap is in the form of a **DIB**). One way of obtaining a handle is to load the image from a file (e.g. a BMP file):\\ \\ +//by Richard Russell, November 2010//\\ \\  This article describes how to save an image as a PNG file. It relies on the presence of the //GDI Plus// library so will work only on Windows XP (or later) or if you have specifically installed **GDIPLUS.DLL** on the target computer. Microsoft permits you to redistribute this file so you can include it with your program if necessary.\\ \\  To begin with you need to have a **handle** to the bitmap you want to save (see below if instead the bitmap is in the form of a **DIB**). One way of obtaining a handle is to load the image from a file (e.g. a BMP file): 
 + 
 +<code bb4w>
         bmpfile$ = "​\Windows\Soap Bubbles.bmp"​         bmpfile$ = "​\Windows\Soap Bubbles.bmp"​
         SYS "​LoadImage",​ 0, bmpfile$, 0, 0, 0, 16 TO hbitmap%         SYS "​LoadImage",​ 0, bmpfile$, 0, 0, 0, 16 TO hbitmap%
-Here the image is loaded at its original size. You can alternatively scale the image to different dimensions:\\ \\ +</​code>​ 
 + 
 +Here the image is loaded at its original size. You can alternatively scale the image to different dimensions: 
 + 
 +<code bb4w>
         bmpfile$ = "​\Windows\Soap Bubbles.bmp"​         bmpfile$ = "​\Windows\Soap Bubbles.bmp"​
         SYS "​LoadImage",​ 0, bmpfile$, 0, dx%, dy%, 16 TO hbitmap%         SYS "​LoadImage",​ 0, bmpfile$, 0, dx%, dy%, 16 TO hbitmap%
-Where **dx%** and **dy%** are the wanted width and height of the image respectively (the scaling quality is not particularly good so for best results you might prefer to scale the image using a third-party program).\\ \\  There are a number of other ways in which you might obtain a bitmap handle, which are outside the scope of this article. You can easily obtain a handle to whatever is displayed in your program'​s output window:\\ \\ +</​code>​ 
 + 
 +Where **dx%** and **dy%** are the wanted width and height of the image respectively (the scaling quality is not particularly good so for best results you might prefer to scale the image using a third-party program).\\ \\  There are a number of other ways in which you might obtain a bitmap handle, which are outside the scope of this article. You can easily obtain a handle to whatever is displayed in your program'​s output window: 
 + 
 +<code bb4w>
         SYS "​GetCurrentObject",​ @memhdc%, 7 TO hbitmap%         SYS "​GetCurrentObject",​ @memhdc%, 7 TO hbitmap%
-but this ordinarily returns the //entire// 1920 x 1440 bitmap which is probably not what you want. To save just a region of your program'​s output window the easiest way is probably to save it first as a BMP file (using ***GSAVE**) then load it using LoadImage as shown above.\\ \\  Once you've got a handle to the bitmap you simply save it as a PNG file as follows:\\ \\ +</​code> ​        
 +         
 +but this ordinarily returns the //entire// 1920 x 1440 bitmap which is probably not what you want. To save just a region of your program'​s output window the easiest way is probably to save it first as a BMP file (using ***GSAVE**) then load it using LoadImage as shown above.\\ \\  Once you've got a handle to the bitmap you simply save it as a PNG file as follows: 
 + 
 +<code bb4w> ​
         PROCsavepng(hbitmap%,​ filename$)         PROCsavepng(hbitmap%,​ filename$)
-Here **filename$** is the name of the PNG file to create.\\ \\  One you've saved the file you should delete the bitmap handle:\\ \\ +</​code>​ 
 + 
 +Here **filename$** is the name of the PNG file to create.\\ \\  One you've saved the file you should delete the bitmap handle: 
 + 
 +<code bb4w>
         SYS "​DeleteObject",​ hbitmap%         SYS "​DeleteObject",​ hbitmap%
-Finally, here's the code for **PROCsavepng** itself:\\ \\ +</​code>​ 
 + 
 +Finally, here's the code for **PROCsavepng** itself: 
 + 
 +<code bb4w>
         DEF PROCsavepng(hbitmap%,​ filename$)         DEF PROCsavepng(hbitmap%,​ filename$)
         LOCAL gdiplus%, ole32%         LOCAL gdiplus%, ole32%
Line 59: Line 81:
  
         ENDPROC         ENDPROC
-\\  ​If, instead of a bitmap **handle**, you have a bitmap (DIB) stored in memory you can use this alternative routine:\\ +</​code>​ 
 + 
 +If, instead of a bitmap **handle**, you have a bitmap (DIB) stored in memory you can use this alternative routine: 
 + 
 +<code bb4w>
         DEF PROCsavepngdib(dib%,​ bmi%, filename$)         DEF PROCsavepngdib(dib%,​ bmi%, filename$)
         LOCAL gdiplus%, ole32%         LOCAL gdiplus%, ole32%
Line 105: Line 131:
  
         ENDPROC         ENDPROC
-\\  ​You would call it as follows:\\ +</​code>​ 
 + 
 +You would call it as follows: 
 + 
 +<code bb4w>
         PROCsavepngdib(dibits%,​ bmi{}, filename$)         PROCsavepngdib(dibits%,​ bmi{}, filename$)
 +</​code>​
 +
 where **dibits%** is the address of the bitmap data and **bmi{}** is a BITMAPINFO structure containing the dimensions etc. and (optionally) colour palette for the bitmap. where **dibits%** is the address of the bitmap data and **bmi{}** is a BITMAPINFO structure containing the dimensions etc. and (optionally) colour palette for the bitmap.
saving_20a_20png_20image.txt · Last modified: 2018/04/13 16:03 by richardrussell