User Tools

Site Tools


loading_20a_20png_20or_20tiff_20image

Differences

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

Link to this comparison view

loading_20a_20png_20or_20tiff_20image [2018/03/31 13:19]
127.0.0.1 external edit
loading_20a_20png_20or_20tiff_20image [2018/04/13 15:47] (current)
richardrussell Added syntax highlighting
Line 3: Line 3:
 //by Richard Russell, October 2008, revised November 2010 and January 2012//\\ \\  The article [[/​Loading%20a%20GIF%20or%20JPEG%20image|Loading a GIF or JPEG image]] lists a replacement for the **LoadImage** API which works for JPEG, GIF, ICO, EMF and WMF images as well as for BMP images. However two common still-image formats not supported by that function are **PNG** and **TIFF**.\\ \\  The two functions listed below each load PNG and TIFF images in addition to the others (since there'​s a large variety of different TIFF formats it may load only the more common variants). They require **GDI+** to be installed on the target PC.\\ \\  //by Richard Russell, October 2008, revised November 2010 and January 2012//\\ \\  The article [[/​Loading%20a%20GIF%20or%20JPEG%20image|Loading a GIF or JPEG image]] lists a replacement for the **LoadImage** API which works for JPEG, GIF, ICO, EMF and WMF images as well as for BMP images. However two common still-image formats not supported by that function are **PNG** and **TIFF**.\\ \\  The two functions listed below each load PNG and TIFF images in addition to the others (since there'​s a large variety of different TIFF formats it may load only the more common variants). They require **GDI+** to be installed on the target PC.\\ \\ 
 ==== Method 1 ==== ==== Method 1 ====
-\\  This version is the simpler of the two, but can't cope with very large images and, when scaling is required, the quality is not as good as it might be:\\ +\\  This version is the simpler of the two, but can't cope with very large images and, when scaling is required, the quality is not as good as it might be: 
 + 
 +<code bb4w>
         DEF FNloadimagegdip(file$,​ RETURN cx%, RETURN cy%, par%)         DEF FNloadimagegdip(file$,​ RETURN cx%, RETURN cy%, par%)
         LOCAL tSI{}, bmp{}, ix%, iy%, hbm%, bmp%, hcopy%, gdip%, text%, res%, lGDIP%         LOCAL tSI{}, bmp{}, ix%, iy%, hbm%, bmp%, hcopy%, gdip%, text%, res%, lGDIP%
Line 58: Line 60:
  
         = hcopy%         = hcopy%
-\\ +</​code>​ 
 ==== Method 2 ==== ==== Method 2 ====
-\\  This version is a little more complicated,​ but can cope with very large images (potentially greater than 10000 x 10000 pixels) and scales the image, when required, with a higher quality:\\ +\\  This version is a little more complicated,​ but can cope with very large images (potentially greater than 10000 x 10000 pixels) and scales the image, when required, with a higher quality: 
 + 
 +<code bb4w>
         DEF FNloadimagegdip2(file$,​ RETURN cx%, RETURN cy%, par%)         DEF FNloadimagegdip2(file$,​ RETURN cx%, RETURN cy%, par%)
         LOCAL tSI{}, bmi{}, text%, gdip%, image%, ix%, iy%, lGDIP%         LOCAL tSI{}, bmi{}, text%, gdip%, image%, ix%, iy%, lGDIP%
Line 129: Line 134:
         SYS "​FreeLibrary",​ gdip%         SYS "​FreeLibrary",​ gdip%
         = hdib%         = hdib%
-\\ +</​code>​ 
 ==== How to use ==== ==== How to use ====
-\\  The parameters to the functions are the filename 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. 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\\ \\ +\\  The parameters to the functions are the filename 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. 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% = FNloadimagegdip2(filename$,​ cx%, cy%, FALSE)         himage% = FNloadimagegdip2(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 FNloadimagegdip2(file$,​ cx%, cy%, par%)         DEF FNloadimagegdip2(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%
 +</​code>​
 +
 To use the **FNloadimagegdip** 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 it is better to use **Method 2** (in which the bitmap is stored in a **DIB Section**). The local variable **bits%** points to the bitmap data, so that could either be changed to a global or returned from the function as another RETURN parameter. The data is in '​top-down'​ format as RGB triplets, although a different format could be used by modifying the contents of the **bmi{}** structure. To use the **FNloadimagegdip** 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 it is better to use **Method 2** (in which the bitmap is stored in a **DIB Section**). The local variable **bits%** points to the bitmap data, so that could either be changed to a global or returned from the function as another RETURN parameter. The data is in '​top-down'​ format as RGB triplets, although a different format could be used by modifying the contents of the **bmi{}** structure.
loading_20a_20png_20or_20tiff_20image.txt · Last modified: 2018/04/13 15:47 by richardrussell