[windows] CatchImage() replacement for bitmaps

Share your advanced PureBasic knowledge/code with the community.
Justin
Addict
Addict
Posts: 948
Joined: Sat Apr 26, 2003 2:49 pm

[windows] CatchImage() replacement for bitmaps

Post by Justin »

Code updated for 5.20+ (same as CatchImage())

I often use the image lib only to include bitmaps inside my exes, i always look for the smallest exe size so i did this proc to get rid of the image lib, not very tested but seems to work.

Code: Select all

;CatchBMP : Creates a compatible bitmap(DDB) from a DIB bitmap inside an exe.
;hdc        : window DC , returned by GetDC() API
;bmpaddress : address of bitmap inside executable
;Returns    : bitmap handle
;Remarks    : use DeleteObject() API with the bitmap handle to free it.
;Justin 07/03 
procedure CatchBMP(hdc,bmpaddress)
  ;ptr to BITMAPFILEHEADER 
  *pbfileh.BITMAPFILEHEADER=bmpaddress

  ;ptr to BITMAPINFOHEADER
  pbinfoh=bmpaddress + sizeof(BITMAPFILEHEADER)

  ;pointer to bitmap data
  pbits=*pbfileh\bfOffBits ;offset
  init=bmpaddress + pbits  ;bitmap data

  procedurereturn CreateDIBitmap_(hdc,pbinfoh,#CBM_INIT,init,pbinfoh,#DIB_RGB_COLORS)
endprocedure
To use it should be something like:

hwnd=openwindow..

hdc=getdc_(hwnd)
hbm=CatchBMP(hdc,?Lbitmap ) ;hbm used as image ID

;free bitmap
deleteobject_(hbm)

Lbitmap : includebinary "c:\yourbitmap.bmp"
Fenix
Enthusiast
Enthusiast
Posts: 102
Joined: Wed May 07, 2003 1:45 am
Location: Germany

Post by Fenix »

Hi!


That´s a question for Fred.

Anyway: Pb only include the funtions needed, it does not include the whole lib!
BUT: PB does also include the Init() and End() - Routine. Init() should be clear -> all the stuff to init the functions in the lib to work.
End() will free used stuff. (Think of files, End() would release the files you opened, but forgot to close).

Init() & End() are actually not the real names! You can compare it to C++ - classes (-> constructor, destructor)


Fenix
Justin
Addict
Addict
Posts: 948
Joined: Sat Apr 26, 2003 2:49 pm

Post by Justin »

I thought the whole lib was included, but using CatchBMP() reduces 3Kb of exe size.
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Post by Pupil »

If the lib isn't splitted the whole lib is included, if however the lib IS splitted only the functions needed/used from that lib is included in the final EXE.

But i see no problem using your method Justin, if you save 3k and that's important for your app then i say -do it!
Fred
Administrator
Administrator
Posts: 18234
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

The PB catch command is of course bigger than this very specific code because of the flexibility you go with it (plugin, bank support, automatic free method at the end etc..). As pupil said, be free to use the best solution which suits for you.
Justin
Addict
Addict
Posts: 948
Joined: Sat Apr 26, 2003 2:49 pm

Post by Justin »

I dropped 3kb in a 70kb app and it's working well, i also use it for icons in this app making an image list with the bitmap, it's even smaller that including the .ico file because the .bmp does not include the masked image and there is an api that makes the icon for you :)
Post Reply