You are on page 1of 4

Load Bitmap to Desired Size

Table of Contents
Load Bitmap to Desired Size
LOADBMP
LoadImageA
Loading at Desired Size
DEMO
LOADBMP
Liberty BASIC's LOADBMP command allows us to load bitmaps into memory. We can th
en draw them in graphics windows or graphicboxes with the DRAWBMP command. They
are displayed in the size in which they were saved to disk. If a bitmap is 100x1
00 pixels, then it is displayed at that size. LOADBMP looks like this when a dis
kfile is loaded:
loadbmp "bmpname", filename.bmp
You can also load images with API calls using either add-on DLLs or the Windows
API. If you load an image with an API call, it returns the handle of the loaded
image. Use that handle with LOADBMP like this:
'bitmap is loaded by API as hBitmap
loadbmp "bmpname", hBitmap
LoadImageA
The LoadImageA API call can load an image on disk into memory. The first argumen
t of the function is for the instance handle. This is not used when loading an i
mage from disk, so we pass it as 0.
The second argument is a string containing the disk filename of the image.
The next argument specifies the type of image. For bitmaps, the Windows constant
"_IMAGE_BITMAP" is used.
The next two arguments are for the desired width and height of the image. If the
se arguments are passed as 0, the image is loaded at the same dimensions as it w
as saved. If these arguments contain values, the image is loaded at those dimens
ions.
The final argument is the load flag. Since we are loading a file from disk, we u
se the Windows constant, "_LR_LOADFROMFILE".
The function returns the memory handle of the loaded bitmap if it is successful,
0 if it fails.
calldll #user32, "LoadImageA",_
0 as ulong,_
'instance - use 0 for image from file
imagePath$ as ptr,_
'path and filename of image
_IMAGE_BITMAP as long,_
'type of image
width as long,_
'desired width
height as long,_
'desired height
_LR_LOADFROMFILE as long,_ 'load flag
hImage as Ulong
'handle of loaded image
Loading at Desired Size
We can load an image so that it is any size we want. This does not change the di
sk file of the image. It only changes the size of the image in memory. We can us
e this method to cause a bitmap to fill a graphicbox, no matter the size of the
graphicbox. The image may be distorted, since the actual width to height proport
ion of the image as saved to disk may differ from the one we use to load it. For
instance, if an image is 100 pixels wide and 200 pixels high on disk, but we lo

ad it to be 100x100, it will appear squashed when displayed. Keep this in mind w


hen using this method. It's a great way to create thumbnail images.
DEMO
Here is a demonstration program. Change the width and height values at the top o
f the code to see how it changes the appearance of the images.
'Use LoadImageA to load bitmaps to be a specified size
nomainwin
width=200 'desired width
height=200 'desired height
menu #1, "&File", "&Open",[open],"E&xit",[quit]
graphicbox #1.g, 0,0,width,height
open "Image Size Test" for window as #1
#1 "trapclose [quit]"
wait
[open]
filedialog "Open Image","*.bmp", imagePath$
if imagePath$="" then wait
if hImage then unloadbmp "image"
calldll #user32, "LoadImageA",_
0 as ulong,_
'instance - use 0 for image from file
imagePath$ as ptr,_
'path and filename of image
_IMAGE_BITMAP as long,_
'type of image
width as long,_
'desired width
height as long,_
'desired height
_LR_LOADFROMFILE as long,_ 'load flag
hImage as Ulong
'handle of loaded image
if hImage = 0 then
notice "Cannot load image."
wait
end if
loadbmp "image", hImage

'get the LB loadbmp name

print #1.g, "down"


print #1.g, "drawbmp image 0 0"
wait
[quit]
if hImage then unloadbmp "image"
close #1:end
==================================
Images in Liberty BASIC
Liberty BASIC provides a command to load a bitmap into memory: LOADBMP. It provi
des a command to display the bitmap in a graphics window or graphicbox: DRAWBMP.
For most purposes, the LOADBMP command suffices. There might be a problem if a
program requires many large bitmap images. Bitmaps can be very large files, espe
cially if they are in 24-bit format or 32-bit format. This makes for a large dis

tribution packet, which can consume server space and take a long time to downloa
d.
JPGs are Smaller
The file size of a jpg image is much smaller than the same image in bitmap forma
t. One such image is 500 pixels wide by 375 pixels high. As a bitmap, the file s
ize is 550 KB. As a jpg, it is only 40 KB. That is a huge difference for a singl
e file, and if several images are to be distributed with a program, the distribu
tion packet size can become unacceptably large. Clearly, the use of jpgs would b
e desirable.
Bad Things About JPGs
Jpg is a "lossy" format. In order to compress the image into a small file size,
algorithms are used to store the image information in fewer bytes. These algorit
hms cause the loss of some of the image information. The greater the compression
, the smaller the file size, and the more loss of data. Jpgs can appear blurry,
or they can have "cast off" pixels. For instance, a white area around a dark fig
ure may be sprinkled with dark pixels near the figure because of the compression
algorithm. Because of this loss of crispness, jpgs are not suitable for some ap
plications. They are not a good choice for sprite images, which need well-define
d borders. Jpgs are a good choice for game backgrouns, though.
The other bad thing about jpgs is that Liberty BASIC 3 has no native commands to
handle jpg images.
The JPEG.DLL
The free jpeg.dll provides a really easy way to load jpg images in Liberty BASIC
. The DLL is only 24 KB, so it adds little to the size of a distribution packet,
while allowing images to be included as jpgs, making for much smaller packets.
Image Formats with the JPEG.DLL
The DLL will load jpg, bmp, gif, ico, wmf and emf formats. Benchmark tests show
that loading a bmp with this DLL is nearly three times as fast as loading it wit
h LOADBMP.
Functions in the DLL
The main function in the DLL is the LoadImageFile function. It requires a window
handle and a disk filename for the image, and it returns the handle of the imag
e in memory. Be sure to make this argument a "ulong"!
open "jpeg.dll" for DLL as #j
calldll #j, "LoadImageFile",_
hWnd as ulong,_
'window handle
file$ as ptr,_
'disk filename of image
ImageHandle as ulong
'handle of image in memory
close #j
The image can then be loaded with a Liberty BASIC LOADBMP command. Once it has b
een loaded by LB, it can be drawn with the DRAWBMP command. It should also be un
loaded with the UNLOADBMP command. (See issue #100 for more on bitmaps in LB.)
'to load:
loadbmp "myPic", ImageHandle
'to unload:
unloadbmp "myPic"
The other two functions provided by the JPEG.DLL are ImageWidth and ImageHeight

that require a handle to the loaded image and return the width or height of the
image as measured in pixels.
open "jpeg.dll" for dll as #j
'input handle of memory bmp
'returns width of image
calldll #j, "ImageWidth",_
hImage as long,_
'handle of image in memory
ImageWidth as ulong
'width of image
'input handle of memory bmp
'returns height of image
calldll #j, "ImageHeight",_
hImage as long,_
'handle of image in memory
ImageHeight as ulong
'height of image
close #j
Sample Program
A complete sample program and the JPEG.DLL are included in the archive for this
newsletter. Here is a quick demo that uses only the image loading function.
nomainwin
filedialog "Open","*.jpg",file$
if file$="" then end
open "jpeg.dll" for DLL as #j
calldll #j, "LoadImageFile",_
hWnd as ulong,_
'window handle
file$ as ptr,_
'disk filename of image
ImageHandle as ulong
'handle of image in memory
close #j
'to load:
loadbmp "myPic", ImageHandle
open "JPG" for graphics as #1
#1 "trapclose [quit]"
#1 "down; drawbmp myPic 0 0;flush"
wait
[quit]
'to unload:
unloadbmp "myPic"
close #1:end

You might also like