This shows you the differences between two versions of the page.
saving_20a_20jpeg_20image [2018/03/31 13:19] 127.0.0.1 external edit |
saving_20a_20jpeg_20image [2018/04/13 16:01] richardrussell Added syntax highlighting |
||
---|---|---|---|
Line 1: | Line 1: | ||
=====Saving a JPEG image===== | =====Saving a JPEG image===== | ||
- | //by Richard Russell, December 2006//\\ \\ The main BBC BASIC for Windows documentation explains how to [[http://www.bbcbasic.co.uk/bbcwin/manual/bbcwine.html#gifjpeg|display a JPEG image]] and elsewhere you can find out how to [[/Displaying%20a%20JPEG%20or%20GIF%20in%20a%20picture%20box|load a JPEG image]] for use in your program. This article describes how to **save** an image as a JPEG 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, December 2006//\\ \\ The main BBC BASIC for Windows documentation explains how to [[http://www.bbcbasic.co.uk/bbcwin/manual/bbcwine.html#gifjpeg|display a JPEG image]] and elsewhere you can find out how to [[/Displaying%20a%20JPEG%20or%20GIF%20in%20a%20picture%20box|load a JPEG image]] for use in your program. This article describes how to **save** an image as a JPEG 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 JPEG 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 JPEG file as follows: | ||
+ | |||
+ | <code bb4w> | ||
PROCsavejpeg(hbitmap%, filename$, quality%) | PROCsavejpeg(hbitmap%, filename$, quality%) | ||
- | Here **filename$** is the name of the JPEG file to create and **quality%** is a measure of relative quality from 1 (bad) to 100 (good). The better the quality the larger the file that will be created.\\ \\ One you've saved the file you should delete the bitmap handle:\\ \\ | + | </code> |
+ | |||
+ | Here **filename$** is the name of the JPEG file to create and **quality%** is a measure of relative quality from 1 (bad) to 100 (good). The better the quality the larger the file that will be created.\\ \\ 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 **PROCsavejpeg** itself:\\ \\ | + | </code> |
+ | |||
+ | Finally, here's the code for **PROCsavejpeg** itself: | ||
+ | |||
+ | <code bb4w> | ||
DEF PROCsavejpeg(hbitmap%, filename$, quality%) | DEF PROCsavejpeg(hbitmap%, filename$, quality%) | ||
LOCAL gdiplus%, ole32% | LOCAL gdiplus%, ole32% | ||
Line 68: | Line 90: | ||
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 PROCsavejpegdib(dib%, bmi%, filename$, quality%) | DEF PROCsavejpegdib(dib%, bmi%, filename$, quality%) | ||
LOCAL gdiplus%, ole32% | LOCAL gdiplus%, ole32% | ||
Line 123: | Line 150: | ||
ENDPROC | ENDPROC | ||
- | \\ You would call it as follows:\\ | + | </code> |
+ | |||
+ | You would call it as follows: | ||
+ | |||
+ | <code bb4w> | ||
PROCsavejpegdib(dibits%, bmi{}, filename$, quality%) | PROCsavejpegdib(dibits%, bmi{}, filename$, quality%) | ||
+ | </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. |