Actually you probably don't want to use that API anyway. The problem with it is that it will do fine for 24bit images with no alpha channel but as soon as you want to use a 32bit image with alpha channel you find that GdipCreateBitmapFromGdiDib won't copy the alpha layer. Your background will be black and there really isn't a good way to correct this. Luckily you have an alternative. You need the BITMAPINFO object and a pointer to the colorbits, right? There's another gdiplus command that doesn't need the BITMAPINFO stuff, just the pointer to the colorbits and some easily-gotten metrics and it willl preserve the alpha channel: GdipCreateBitmapFromScan0. So I'll show you how to use that one instead:
Code: Select all
#MatrixOrderPrepend = 0
#MatrixOrderAppend = 1
#PixelFormat32bppARGB = $26200A
Import "gdiplus.lib"
GdiplusStartup(a,b,c)
GdiplusShutdown(a)
GdipDrawImageRectI(a,b,c,d,e,f)
GdipDisposeImage(a)
GdipCreateFromHDC(a,b)
GdipDeleteGraphics(a)
GdipReleaseDC(a,b)
GdipCreateBitmapFromScan0(a,b,c,d,e,f)
GdipRotateWorldTransform(a, b.f, c)
GdipTranslateWorldTransform(a, b.f, c.f, d)
GdipResetWorldTransform(a)
EndImport
Structure GdiplusStartupInput
GdiPlusVersion.l
*DebugEventCallback.DEBUG_EVENT
SuppressBackgroundThread.l
SuppressExternalCodecs.l
EndStructure
CreateImage(1, 256, 256, 32, #PB_Image_Transparent)
StartDrawing(ImageOutput(1))
DrawingMode(#PB_2DDrawing_AllChannels)
Box(56,56,144,144,RGBA(0,0,210,255))
StopDrawing()
; Initialize GDIPlus
input.GdiplusStartupInput\GdiPlusVersion = 1
GdiplusStartup(@*token, @input, #Null)
; Create our bitmap
GetObject_(ImageID(1), SizeOf(BITMAP), @bmp.BITMAP)
GdipCreateBitmapFromScan0(256, 256, bmp\bmWidthBytes, #PixelFormat32bppARGB, bmp\bmBits, @*gdip_bitmap)
; Now let's have some fun with it:
OpenWindow(0,0,0,256,256,"GdipCreateBitmapFromScan0 Demo",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
AddWindowTimer(0,1,5)
CanvasGadget(0,0,0,256,256)
angle.f = 0
Repeat
EventID = WaitWindowEvent()
Select EventID
Case #PB_Event_Timer
CreateImage(0, 256, 256, 32, #PB_Image_Transparent)
hDC = StartDrawing(CanvasOutput(0))
Box(0,0,256,256,GetSysColor_(#COLOR_3DFACE))
GdipCreateFromHDC(hDC, @*gfxcontext)
GdipRotateWorldTransform(*gfxcontext, angle, #MatrixOrderPrepend)
GdipTranslateWorldTransform(*gfxcontext, 128, 128, #MatrixOrderAppend)
GdipDrawImageRectI(*gfxcontext, *gdip_bitmap, 128.0, 128.0, -256, -256)
GdipReleaseDC(*gfxcontext, hDC)
GdipDeleteGraphics(*gfxcontext)
StopDrawing()
angle+0.1:If angle>=360:angle=0:EndIf
EndSelect
Until EventID = #PB_Event_CloseWindow
GdiplusShutdown(*token)