saving_20a_20png_20image
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
saving_20a_20png_20image [2018/03/31 13:19] – external edit 127.0.0.1 | saving_20a_20png_20image [2024/01/05 00:21] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
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$ = " | bmpfile$ = " | ||
SYS " | SYS " | ||
- | Here the image is loaded at its original size. You can alternatively scale the image to different dimensions:\\ \\ | + | </ |
+ | |||
+ | Here the image is loaded at its original size. You can alternatively scale the image to different dimensions: | ||
+ | |||
+ | <code bb4w> | ||
bmpfile$ = " | bmpfile$ = " | ||
SYS " | SYS " | ||
- | 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' | + | </ |
+ | |||
+ | 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' | ||
+ | |||
+ | <code bb4w> | ||
SYS " | SYS " | ||
- | 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' | + | </ |
+ | |||
+ | 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' | ||
+ | |||
+ | <code bb4w> | ||
PROCsavepng(hbitmap%, | PROCsavepng(hbitmap%, | ||
- | Here **filename$** is the name of the PNG file to create.\\ \\ One you've saved the file you should delete the bitmap handle:\\ \\ | + | </ |
+ | |||
+ | 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 " | SYS " | ||
- | Finally, here's the code for **PROCsavepng** itself:\\ \\ | + | </ |
+ | |||
+ | Finally, here's the code for **PROCsavepng** itself: | ||
+ | |||
+ | <code bb4w> | ||
DEF PROCsavepng(hbitmap%, | DEF PROCsavepng(hbitmap%, | ||
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 bb4w> | ||
DEF PROCsavepngdib(dib%, | DEF PROCsavepngdib(dib%, | ||
LOCAL gdiplus%, ole32% | LOCAL gdiplus%, ole32% | ||
Line 105: | Line 131: | ||
ENDPROC | ENDPROC | ||
- | \\ | + | </ |
+ | |||
+ | You would call it as follows: | ||
+ | |||
+ | <code bb4w> | ||
PROCsavepngdib(dibits%, | PROCsavepngdib(dibits%, | ||
+ | </ | ||
+ | |||
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.1522502378.txt.gz · Last modified: 2024/01/05 00:16 (external edit)