Icon as Bitmap

New to FreeBASIC? Post your questions here.
Post Reply
bojan.dosen
Posts: 166
Joined: May 14, 2007 12:20
Location: Zagreb, Croatia

Icon as Bitmap

Post by bojan.dosen »

How can I load Icon (.ico file) as bitmap? Is it possible, and how?
Thanks!
1000101
Posts: 2556
Joined: Jun 13, 2005 23:14
Location: SK, Canada

Post by 1000101 »

Well, freebasic doesn't have a direct means to load ico files, but you can do it yourself once you know the file format.

www.wotsit.org
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Post by MichaelW »

One way would be to convert the icon to a bitmap and load the bitmap. This is a quick and dirty test app that uses GDI+ to do the conversion. It seems to work OK, but I tested it on only one icon.

Code: Select all

#include "win/gdiplus.bi"
'=====================================================================
using gdiplus

dim as GdiplusStartupInput gdipsi
dim as ULONG_PTR gdipToken
dim as GpImage ptr image
dim as wstring * 11 fnico = "fblogo.ico"
dim as wstring * 11 fnbmp = "fblogo.bmp"
dim as ImageCodecInfo ptr pImageCodecInfo
dim as UINT num, size

gdipsi.GdiplusVersion = 1

print GdiplusStartup( @gdipToken, @gdipsi, null )

print GdipLoadImageFromFile( @fnico, @image )

print GdipGetImageEncodersSize( @num, @size )

pImageCodecInfo = allocate( num )

print GdipGetImageEncoders( num, size, pImageCodecInfo )

for i as integer = 0 to num - 1
  print *cast(wstring ptr,pImageCodecInfo[i].MimeType)
  if *cast(wstring ptr,pImageCodecInfo[i].MimeType) = "image/bmp" then
    print GdipSaveImageToFile( image, @fnbmp, _
                               @pImageCodecInfo[i].Clsid, null )
  end if
next

print GdipDisposeImage( image )

GdiplusShutdown( gdipToken )

deallocate( pImageCodecInfo )

sleep

/'
enum Status
	Ok = 0
	GenericError = 1
	InvalidParameter = 2
	OutOfMemory = 3
	ObjectBusy = 4
	InsufficientBuffer = 5
	NotImplemented = 6
	Win32Error = 7
	WrongState = 8
	Aborted = 9
	FileNotFound = 10
	ValueOverflow = 11
	AccessDenied = 12
	UnknownImageFormat = 13
	FontFamilyNotFound = 14
	FontStyleNotFound = 15
	NotTrueTypeFont = 16
	UnsupportedGdiplusVersion = 17
	GdiplusNotInitialized = 18
	PropertyNotFound = 19
	PropertyNotSupported = 20
end enum

type ImageCodecInfo
	Clsid as CLSID
	FormatID as GUID
	CodecName as WCHAR ptr
	DllName as WCHAR ptr
	FormatDescription as WCHAR ptr
	FilenameExtension as WCHAR ptr
	MimeType as WCHAR ptr
	Flags as DWORD
	Version as DWORD
	SigCount as DWORD
	SigSize as DWORD
	SigPattern as BYTE ptr
	SigMask as BYTE ptr
end type

GpStatus WINGDIPAPI GdipSaveImageToFile(
    GpImage *image,
    GDIPCONST WCHAR* filename,
    GDIPCONST CLSID* clsidEncoder,
    GDIPCONST EncoderParameters* encoderParams
);
'/
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Post by srvaldez »

MichaelW, where can I find the gdiplus library? don't have it, looked at the links section but all I saw was gd, not gdiplus.
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

I think (not a winapi pro) that you automatically have that library with windows
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Post by srvaldez »

perhaps but there's no import lib in my FB distribution, did a search on my HD and found the gdiplus.dll, I can make the import lib.
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Post by srvaldez »

now I get a bunch of undeclared function errors
C:\Program Files\FreeBASIC\examples\ico to bmp.o:fake:(.text+0x9a): undefined reference to `GdiplusStartup@12'
C:\Program Files\FreeBASIC\examples\ico to bmp.o:fake:(.text+0xb1): undefined reference to `GdipLoadImageFromFile@8'
C:\Program Files\FreeBASIC\examples\ico to bmp.o:fake:(.text+0xc8): undefined reference to `GdipGetImageEncodersSize@8'
C:\Program Files\FreeBASIC\examples\ico to bmp.o:fake:(.text+0xee): undefined reference to `GdipGetImageEncoders@12'
C:\Program Files\FreeBASIC\examples\ico to bmp.o:fake:(.text+0x15d): undefined reference to `GdipSaveImageToFile@16'
C:\Program Files\FreeBASIC\examples\ico to bmp.o:fake:(.text+0x17a): undefined reference to `GdipDisposeImage@4'
C:\Program Files\FreeBASIC\examples\ico to bmp.o:fake:(.text+0x18a): undefined reference to `GdiplusShutdown@4'
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Post by MichaelW »

Sorry, I was assuming that the problem with the missing import library was just with my FB installation.

To build the import library I downloaded gdiplus.dll.def from here and ran this batch file:

Code: Select all

dlltool -k -d gdiplus.dll.def -l libgdiplus.dll.a
pause
For anyone who does not have GDIPLUS.DLL, it is available here. AFAIK the DLL is included with Windows XP and later.

BTW undefined <> undeclared
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Post by srvaldez »

thanks MichaelW, the program compiles and runs but crashes at the end, here's the output.
0
0
0
0
image/bmp
0
image/jpeg
image/gif
image/png
0
when I press Return key to end the program windows reports an Aplication error:
The instruction "0x5ad776a3" referenced memory at "0xb96b3cdf". The memory could not be "read".
Click OK to terminate the program
also if I add to the print statements

Code: Select all

Print "1",GdiplusStartup( @gdipToken, @gdipsi, null )
Print "2",GdipLoadImageFromFile( @fnico, @image )
'etc
the program exits without waiting.
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Post by MichaelW »

It works for me under Windows 2000 and XP SP2, but under XP for some reason the sleep statement does not cause it to pause before closing. How big is your import library, and what version of FB are you using?
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Post by srvaldez »

I used the dll in your link also the CVS gdiplus.dll.def
import lib size 476,450 bytes
size on disk 491,520 bytes
running WindowsXP SP2 latest updates, FB version 0.18 (5-18-07)
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Post by MichaelW »

The size matches that of the import library that I built. I have now tested the 0.17 release version and the CVS from May 27, and the program works as it did before. Turning on DEP for all programs did not change the results. The program terminating without pausing on my XP system could possibly indicate a problem, but I get no error messages and the event logs show nothing related to the program. If you are using Windows 2000 or later, the event logs might provide some useful information.
rdc
Posts: 1741
Joined: May 27, 2005 17:22
Location: Texas, USA
Contact:

Post by rdc »

It works fine on my Win2K box. You might want to do a full reinstall of FB and make sure you install all the libraries. You probably did this already, but if not, you could at least rule out any install problems.
Post Reply