[RESOLVED] Minimum code to draw PNG? GDI+?
What's the minimum code I can use to draw a PNG with alpha transparency (of course) to a window/device context?
ಠ_ಠ
Edit: Let me add that I'm making a 2D side-scrolling game and want to be able to use PNGs. I might also have to have collision detection while taking alpha transparency into effect. :sick: Any ideas on this?
Re: Minimum code to draw PNG? GDI+?
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
Re: Minimum code to draw PNG? GDI+?
Thanks, LaVolpe. :) Figured you'd be the one to answer. :duck:
ಠ_ಠ
Re: Minimum code to draw PNG? GDI+?
Another interesting thread....
http://www.vbaccelerator.com/home/vb...on/article.asp
Edit:
Yeah LaVolpe is good with that :)
Re: Minimum code to draw PNG? GDI+?
Quote:
Originally Posted by
DigiRev
Thanks, LaVolpe. :) Figured you'd be the one to answer. :duck:
ಠ_ಠ
You're welcome. I did add a worthwhile GDI+ bookmark to my reply and also turned off On Error Resume Next in the sample code. Never want to leave that on indefinitely
Re: [RESOLVED] Minimum code to draw PNG? GDI+?
btw: is there an API to saveimagefile ?
or i have to know the png format for example
and save it bit by bit ?
Re: [RESOLVED] Minimum code to draw PNG? GDI+?
Quote:
Originally Posted by
whatsup
btw: is there an API to saveimagefile ?
or i have to know the png format for example
and save it bit by bit ?
See this thread in the CodeBank section. You'll have to extract what you need or simply use it as a guide to rebuild the SaveAs routine you need. The thread has examples of saving as bitmap, jpg, png & tiff.
Re: [RESOLVED] Minimum code to draw PNG? GDI+?
I found this http://www.almostfreeware.com/prod/1001/index.php while searching for LaVolpe AlphaImage Control.
Is this commercial version of LaVolpe Control or somebody has older version and tries to make money from selling someone else's work?
Re: [RESOLVED] Minimum code to draw PNG? GDI+?
Re: [RESOLVED] Minimum code to draw PNG? GDI+?
Thread closed as it's older than Methusalah. Please don't dig up old threads unless you're going to add something really useful to them.:)