Older Version
Newer Version
JanetTerra
Dec 21, 2009
Load Any Image with GDIPlus
Up until recently a third party DLL was required to load non-bitmap images. With GDIPlus, this is no longer the case. Dan Teel wrote this bit of code and released it into Public Domain. GDIPlus will load bmp, gif, ico, jpg, png, and tiff formats. The loaded images are then given a bitmap handle and can then be manipulated by Liberty BASIC as any other loaded bitmap. When the image is no longer needed in memory, the image should be released with DeleteObject and the LB image memory released with an Unloadbmp command.
nomainwin
WindowWidth = 300
WindowHeight = 300
UpperLeftX=int((DisplayWidth-WindowWidth)/2)
UpperLeftY=int((DisplayHeight-WindowHeight)/2)
graphicbox #main.g, -1, -1, 302, 302
open "Picture load" for window_nf as #main
print #main.g, "down; fill white; flush"
print #main, "font ms_sans_serif 10"
print #main, "trapclose [quit.main]"
open "gdiplus.dll" for dll as #gdip
filedialog "Choose an image","*.jpg;*.png;*.tiff;*.ico;*.bmp;*.gif",file$
if file$="" then
goto [quit.main]
else
hBmp=LoadImgFromFile(file$)
if hBmp<>0 then
loadbmp "pic",hBmp
print #main.g, "drawbmp pic 0 0;flush"
unloadbmp "pic"
calldll #gdi32,"DeleteObject",hBmp as ulong, ret as ulong'Because LB doesnt delete it for us.
else
notice "Could not load the image file!"
end if
end if
wait
[quit.main]
close #gdip
close #main
end
function wchar$(string$)
for i = 1 to len(string$)
wchar$=wchar$+mid$(string$,i,1)+chr$(0)
next i
wchar$=wchar$+chr$(0)+chr$(0)
end function
function LoadImgFromFile(file$)
struct dword,a as ulong
gdistart$=chr$(1)
for i = 1 to 15
gdistart$=gdistart$+chr$(0)
next i
calldll #gdip,"GdiplusStartup",dword as struct,gdistart$ as ptr,status as ulong
token=dword.a.struct
if status<>0 then
LoadImgFromFile=0
else
wFileLoc$=wchar$(file$)
calldll #gdip,"GdipCreateBitmapFromFile",wFileLoc$ as ptr,dword as struct,status as ulong
hPic=dword.a.struct
if status<>0 then
LoadImgFromFile=0
else
calldll #gdip,"GdipCreateHBITMAPFromBitmap",hPic as ulong,dword as struct,0 as ulong,status as ulong
hBmp=dword.a.struct
if status<>0 then
LoadImgFromFile=0
else
LoadImgFromFile=hBmp
end if
calldll #gdip,"GdipDisposeImage",hPic as ulong,ret as ulong
end if
calldll #gdip,"GdiplusShutdown",token as ulong,ret as ulong
end if
end function
Thanks, Dan!