Page 1 of 1

[windows] CatchImage() replacement for bitmaps

Posted: Thu Jul 10, 2003 8:26 pm
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"

Posted: Fri Jul 11, 2003 12:16 pm
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

Posted: Fri Jul 11, 2003 2:15 pm
by Justin
I thought the whole lib was included, but using CatchBMP() reduces 3Kb of exe size.

Posted: Fri Jul 11, 2003 2:33 pm
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!

Posted: Fri Jul 11, 2003 4:31 pm
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.

Posted: Fri Jul 11, 2003 7:10 pm
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 :)