User Tools

Site Tools


loading_20a_20gif_20or_20jpeg_20image

Differences

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

Link to this comparison view

loading_20a_20gif_20or_20jpeg_20image [2018/03/31 13:19]
127.0.0.1 external edit
loading_20a_20gif_20or_20jpeg_20image [2018/04/13 15:45] (current)
richardrussell Added syntax highlighting
Line 1: Line 1:
 =====Loading a GIF or JPEG image===== =====Loading a GIF or JPEG image=====
  
-//by Richard Russell, September 2008//\\ \\  The main Help documentation describes how to [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwine.html#​gifjpeg|display a GIF or JPEG image]] on the main output window, but you may wish to perform other operations on an image such as accessing its data or displaying it in a static control. In such cases you are likely to need a **handle** to the image.\\ \\  The function below operates in a similar way to **SYS "​LoadImage"​** except that it works with JPEG, GIF, BMP, ICO, EMF or WMF images. You can alternatively use the function **FNloadimagegdip2** listed at [[/​Loading%20a%20PNG%20or%20TIFF%20image|Loading a PNG or TIFF image]] because that will load GIF and JPEG images too, and gives better results when the image is very large or needs to be scaled.\\ \\ +//by Richard Russell, September 2008//\\ \\  The main Help documentation describes how to [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwine.html#​gifjpeg|display a GIF or JPEG image]] on the main output window, but you may wish to perform other operations on an image such as accessing its data or displaying it in a static control. In such cases you are likely to need a **handle** to the image.\\ \\  The function below operates in a similar way to **SYS "​LoadImage"​** except that it works with JPEG, GIF, BMP, ICO, EMF or WMF images. You can alternatively use the function **FNloadimagegdip2** listed at [[/​Loading%20a%20PNG%20or%20TIFF%20image|Loading a PNG or TIFF image]] because that will load GIF and JPEG images too, and gives better results when the image is very large or needs to be scaled. 
 + 
 +<code bb4w>
         DEF FNloadimage(file$,​ RETURN cx%, RETURN cy%, par%)         DEF FNloadimage(file$,​ RETURN cx%, RETURN cy%, par%)
         LOCAL iid{}, bmp{}, ix%, iy%, hbm%, hcopy%, pic%, ole%, olpp%, text%         LOCAL iid{}, bmp{}, ix%, iy%, hbm%, hcopy%, pic%, ole%, olpp%, text%
Line 42: Line 44:
  
         = hcopy%         = hcopy%
-The parameters to the function are the filename (or URL) of the image, the required width and height (in pixels) and a flag to indicate whether you want the **aspect ratio** to be preserved or not. Note that a filename must contain the drive name (e.g. **C:**) to distinguish it from an internet URL. The returned value is a **handle** to the image, or zero if the image cannot be found or opened.\\ \\  If the required width and/or height values are supplied as zero then the actual width and/or height of the original image are used. Thus if a non-zero value is supplied for the width and zero is supplied for the height, the image will be scaled horizontally (if required) but not vertically; however if the **preserve aspect ratio** flag is set to TRUE, the then image //will// be scaled vertically to preserve the correct shape.\\ \\  The actual dimensions of the (possibly scaled) image are returned in the second and third parameters, therefore they must be supplied as **variables** rather than as **constants**. Thus if you don't want to scale the image call the function as follows\\ \\ +</​code>​ 
 + 
 +The parameters to the function are the filename (or URL) of the image, the required width and height (in pixels) and a flag to indicate whether you want the **aspect ratio** to be preserved or not. Note that a filename must contain the drive name (e.g. **C:**) to distinguish it from an internet URL. The returned value is a **handle** to the image, or zero if the image cannot be found or opened.\\ \\  If the required width and/or height values are supplied as zero then the actual width and/or height of the original image are used. Thus if a non-zero value is supplied for the width and zero is supplied for the height, the image will be scaled horizontally (if required) but not vertically; however if the **preserve aspect ratio** flag is set to TRUE, the then image //will// be scaled vertically to preserve the correct shape.\\ \\  The actual dimensions of the (possibly scaled) image are returned in the second and third parameters, therefore they must be supplied as **variables** rather than as **constants**. Thus if you don't want to scale the image call the function as follows 
 + 
 +<code bb4w>
         cx% = 0         cx% = 0
         cy% = 0         cy% = 0
         himage% = FNloadimage(filename$,​ cx%, cy%, FALSE)         himage% = FNloadimage(filename$,​ cx%, cy%, FALSE)
-Alternatively if you are not interested in knowing the actual dimensions you can modify the function definition by removing the **RETURN** qualifiers:\\ \\ +</​code>​ 
 + 
 +Alternatively if you are not interested in knowing the actual dimensions you can modify the function definition by removing the **RETURN** qualifiers: 
 + 
 +<code bb4w>
         DEF FNloadimage(file$,​ cx%, cy%, par%)         DEF FNloadimage(file$,​ cx%, cy%, par%)
-With this modification you can supply constant values (e.g. 0) as parameters.\\ \\  When you have completely finished with the image you should delete the handle as follows:\\ \\ +</​code>​ 
 + 
 +With this modification you can supply constant values (e.g. 0) as parameters.\\ \\  When you have completely finished with the image you should delete the handle as follows: 
 + 
 +<code bb4w>
         SYS "​DeleteObject",​ himage%         SYS "​DeleteObject",​ himage%
-To use the **FNloadimage** function to display an image in a **static control** see this [[/​Displaying%20a%20JPEG%20or%20GIF%20in%20a%20picture%20box|article]].\\ \\  To access the data of the image you can use the **SYS "​GetDIBits"​** API as follows:\\ \\ +</​code>​ 
 + 
 +To use the **FNloadimage** function to display an image in a **static control** see this [[/​Displaying%20a%20JPEG%20or%20GIF%20in%20a%20picture%20box|article]].\\ \\  To access the data of the image you can use the **SYS "​GetDIBits"​** API as follows: 
 + 
 +<code bb4w>
         cx% = 0         cx% = 0
         cy% = 0         cy% = 0
Line 73: Line 91:
         SYS "​DeleteObject",​ himage%         SYS "​DeleteObject",​ himage%
         IF num% <> cy% ERROR 100, "​GetDIBits failed"​         IF num% <> cy% ERROR 100, "​GetDIBits failed"​
-This provides the image data in '​top-down'​ format as RGB triplets stored at **imagedata%**;​ if you require a different data format modify the members of the **bmih** structure accordingly. Note that each line of image data is always padded (if necessary) to a multiple of four bytes.\\ \\  If the image data may be too large to be stored on the heap (possibly with **HIMEM** increased) then you can instead allocate the memory using the Windows API:\\ \\ +</​code>​ 
 + 
 +This provides the image data in '​top-down'​ format as RGB triplets stored at **imagedata%**;​ if you require a different data format modify the members of the **bmih** structure accordingly. Note that each line of image data is always padded (if necessary) to a multiple of four bytes.\\ \\  If the image data may be too large to be stored on the heap (possibly with **HIMEM** increased) then you can instead allocate the memory using the Windows API: 
 + 
 +<code bb4w>
         SYS "​GlobalAlloc",​ &40, bmih.biSizeImage% TO imagedata%         SYS "​GlobalAlloc",​ &40, bmih.biSizeImage% TO imagedata%
         IF imagedata% = 0 ERROR 100, "​Insufficient memory"​         IF imagedata% = 0 ERROR 100, "​Insufficient memory"​
 +</​code>​
 +
 but in this case don't forget to free the memory later. but in this case don't forget to free the memory later.
loading_20a_20gif_20or_20jpeg_20image.txt · Last modified: 2018/04/13 15:45 by richardrussell