Code:
Private Declare Function GdiplusStartup Lib "gdiplus" (Token As Long, inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As Long = 0) As Long
Private Declare Function GdipLoadImageFromFile Lib "GdiPlus.dll" (ByVal mFilename As Long, ByRef mImage As Long) As Long
Private Declare Function GdipDeleteGraphics Lib "GdiPlus.dll" (ByVal mGraphics As Long) As Long
Private Declare Function GdipCreateFromHDC Lib "gdiplus" (ByVal hDC As Long, hGraphics As Long) As Long
Private Declare Function GdipDrawImage Lib "GdiPlus.dll" (ByVal mGraphics As Long, ByVal mImage As Long, ByVal mX As Single, ByVal mY As Single) As Long
Private Declare Function GdipDisposeImage Lib "gdiplus" (ByVal Image As Long) As Long
Private Declare Sub GdiplusShutdown Lib "gdiplus" (ByVal Token As Long)
Private Type GdiplusStartupInput
GdiplusVersion As Long
DebugEventCallback As Long
SuppressBackgroundThread As Long
SuppressExternalCodecs As Long
End Type
Private Function RenderPNG(FileName As String, hDC As Long, X As Long, Y As Long) As Boolean
On Error Resume Next
Dim GDIsi As GdiplusStartupInput, gToken As Long, hGraphics As Long, hBitmap As Long
GDIsi.GdiplusVersion = 1&
GdiplusStartup gToken, GDIsi
If Err Then
Err.Clear
Exit Function
ElseIf gToken = 0& Then
Exit Function
End If
On Error Goto 0
Call GdipCreateFromHDC(hDC, hGraphics)
If hGraphics Then
Call GdipLoadImageFromFile(StrPtr(FileName), hBitmap)
If hBitmap Then
GdipDrawImage hGraphics, hBitmap, X, Y
GdipDisposeImage hBitmap
RenderPNG = True
End If
GdipDeleteGraphics hGraphics
End If
GdiplusShutdown gToken
End Function
The above code is about as basic is it gets. Not much room for customization. To render the image at different sizes, more API calls needed to get the image size and a different draw API needed to render at different sizes: GdipGetImageBounds & GdipDrawImageRectRectI APIs respectively
Other tips:
-- The gdi+ token, especially in a game, is best to be retrieved at form load and released in form terminate. You really don't want to take extra cpu cycles creating/destroying it every time you load/draw an image
-- The hBitmap should be cached too as long as the game piece is being moved; obviously loading the image every time you want to paint it is wasting time.
Worth bookmarking: http://www.jose.it-berater.org/gdiplus/iframe/index.htm