-
Sep 2nd, 2009, 08:10 AM
#1
[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. Any ideas on this?
Last edited by DigiRev; Sep 2nd, 2009 at 08:14 AM.
-
Sep 2nd, 2009, 08:43 AM
#2
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
Last edited by LaVolpe; Sep 2nd, 2009 at 08:50 AM.
Reason: added bookmark url
-
Sep 2nd, 2009, 08:46 AM
#3
Re: Minimum code to draw PNG? GDI+?
Thanks, LaVolpe. Figured you'd be the one to answer.
ಠ_ಠ
-
Sep 2nd, 2009, 08:46 AM
#4
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
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Sep 2nd, 2009, 08:50 AM
#5
Re: Minimum code to draw PNG? GDI+?
Originally Posted by DigiRev
Thanks, LaVolpe. Figured you'd be the one to answer.
ಠ_ಠ
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
-
Oct 26th, 2009, 04:38 PM
#6
Hyperactive Member
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 ?
-
Feb 27th, 2010, 01:22 PM
#7
Re: [RESOLVED] Minimum code to draw PNG? GDI+?
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.
-
Jun 20th, 2010, 06:41 PM
#8
New Member
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?
-
Nov 4th, 2014, 10:10 AM
#9
Junior Member
Re: [RESOLVED] Minimum code to draw PNG? GDI+?
Women forgive but never forget. Men forget but never forgive.
-
Nov 5th, 2014, 03:03 AM
#10
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.
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|