Page 1 of 1

PB Image from HBITMAP

Posted: Thu Jul 19, 2007 1:54 am
by r_hyde
Been playing with some ideas; thought I'd share this one with the crowd:

Code: Select all

; I found a PB_StructureDataBase structure and example somewhere 
; on the PB English forum, and got the idea that it might be useful 
; for replacing the HBITMAP in a PB image with an HBITMAP obtained
; elsewhere.  This appears to work fine, but I assume it may not be
; future-proof.  I'm really not even sure how safe this is, but it
; sure seems to do what I need it to!

; Thanks go To whoever found out this structure and how to apply it
Structure PB_StructureDataBase 
  Bitmap.l
  Width.w
  Height.w 
  Depth.w 
  ColorArray.l
EndStructure

Procedure.l Hbitmap2PBImage(hbitmap.l)
  Protected *idb.PB_StructureDataBase
  GetObject_(hbitmap, SizeOf(BITMAP), @bm.BITMAP)
  If bm\bmBits
    *idb = CreateImage(#PB_Any, bm\bmWidth, bm\bmHeight, bm\bmBitsPixel)
    If IsImage(*idb)
      DeleteObject_(*idb\Bitmap)
      *idb\Bitmap = hbitmap
    EndIf
  EndIf
  ProcedureReturn *idb
EndProcedure

; This test is a bit pointless, but I'm sure you clever lot will
; be able to imagine a better way to implement the idea...
img1 = CreateImage(#PB_Any, 256, 256, 24)
img2 = Hbitmap2PBImage(ImageID(img1))

; img1 and img2 now share the same HBITMAP!  Let's play with this
; by drawing onto img1 and then displaying img2:
StartDrawing(ImageOutput(img1))
  Box(0, 0, 256, 256, #White)
  DrawText(20, 20, "PureBasic is #1!", #Red)
StopDrawing()

OpenWindow(0, 0, 0, 256, 256, "Bitmap Fun", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
CreateGadgetList(WindowID(0))
ImageGadget(0, 0, 0, 256, 256, ImageID(img2))

Repeat : Until WindowEvent() = #PB_Event_CloseWindow
Have fun with it, and let me know what you like/don't like about it. I'd also like to hear if anyone is able to do anything really cool with this idea! In particular, localmotion34 might be able to incorporate this into PBXimage so that its clients can work with the different supported formats in a PB-native way. Whaddya think?

Posted: Thu Jul 19, 2007 4:00 am
by PureLust
Nice1. ;)

About you Demo-Code above:
I know, Hbitmap2PBImage() is not designed to use the Bitmap of a PB-Image for another BP-Image as you do in you Code above.
But if you do this, you have to be aware, that both Images will be invalid after you free just one of them.

Posted: Thu Jul 19, 2007 5:27 am
by r_hyde
Thx PureLust! Like you suspected, I didn't really have that example in mind when I struck out for a way to put an HBITMAP to a PB image, it was just an easy little example to code.