User Tools

Site Tools


saving_20a_20gif_20image

Differences

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

Link to this comparison view

saving_20a_20gif_20image [2018/03/31 13:19]
127.0.0.1 external edit
saving_20a_20gif_20image [2018/04/13 15:58] (current)
richardrussell Added syntax highlighting
Line 1: Line 1:
 =====Saving a GIF image===== =====Saving a GIF 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 GIF image]] and elsewhere you can find out how to [[/​Displaying%20a%20JPEG%20or%20GIF%20in%20a%20picture%20box|load a GIF image]] for use in your program. This article describes how to **save** an image as a GIF 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 GIF image]] and elsewhere you can find out how to [[/​Displaying%20a%20JPEG%20or%20GIF%20in%20a%20picture%20box|load a GIF image]] for use in your program. This article describes how to **save** an image as a GIF 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%
Line 7: Line 9:
         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.\\ \\  Note that a GIF file has a maximum of 256 different colours, and if you use the above methods a colour palette consisting of a standard selection of colours will be used. The palette will **not** be optimised to the actual set of colours present in the source image. This can result in the quality of the GIF image being significantly worse than it needs to be.\\ \\  If this is important you should first convert the image to an 8-bit (256-colour) BMP file, using an appropriate image editor capable of synthesising an optimised custom palette (for example using Heckbert quantisation). You should then load that BMP file as follows (using LoadImage will //not// preserve the palette):\\ \\ +</​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.\\ \\  Note that a GIF file has a maximum of 256 different colours, and if you use the above methods a colour palette consisting of a standard selection of colours will be used. The palette will **not** be optimised to the actual set of colours present in the source image. This can result in the quality of the GIF image being significantly worse than it needs to be.\\ \\  If this is important you should first convert the image to an 8-bit (256-colour) BMP file, using an appropriate image editor capable of synthesising an optimised custom palette (for example using Heckbert quantisation). You should then load that BMP file as follows (using LoadImage will //not// preserve the palette): 
 + 
 +<code bb4w>
         bmpfile$ = "​tcf.bmp"​         bmpfile$ = "​tcf.bmp"​
         bmpfile% = OPENIN(bmpfile$)         bmpfile% = OPENIN(bmpfile$)
Line 24: Line 34:
           vbits%!I% = bmpfile%!(I%+O%)           vbits%!I% = bmpfile%!(I%+O%)
         NEXT         NEXT
-Once you've got a handle to the bitmap you simply save it as a GIF file as follows:\\ \\ +</​code>​ 
 + 
 +Once you've got a handle to the bitmap you simply save it as a GIF file as follows: 
 + 
 +<code bb4w>
         PROCsavegif(hbitmap%,​ filename$)         PROCsavegif(hbitmap%,​ filename$)
-Here **filename$** is the name of the GIF file to create.\\ \\  One you've saved the file you should delete the bitmap handle:\\ \\ +</​code>​ 
 + 
 +Here **filename$** is the name of the GIF 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 **PROCsavegif** itself:\\ \\ +</​code>​ 
 + 
 +Finally, here's the code for **PROCsavegif** itself: 
 + 
 +<code bb4w>
         DEF PROCsavegif(hbitmap%,​ filename$)         DEF PROCsavegif(hbitmap%,​ filename$)
         LOCAL gdiplus%, ole32%         LOCAL gdiplus%, ole32%
Line 74: Line 96:
  
         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 PROCsavegifdib(dib%,​ bmi%, filename$)         DEF PROCsavegifdib(dib%,​ bmi%, filename$)
         LOCAL gdiplus%, ole32%         LOCAL gdiplus%, ole32%
Line 120: Line 146:
  
         ENDPROC         ENDPROC
-\\  ​You would call it as follows:\\ +</​code>​ 
 + 
 +You would call it as follows: 
 + 
 +<code bb4w>
         PROCsavegifdib(dibits%,​ bmi{}, filename$)         PROCsavegifdib(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_20gif_20image.txt · Last modified: 2018/04/13 15:58 by richardrussell