[RESOLVED] Minimum code to draw PNG? GDI+?-VBForums
Results 1 to 10 of 10

Thread: [RESOLVED] Minimum code to draw PNG? GDI+?

  1. #1

    Thread Starter
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Resolved [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 09:14 AM.
    Share on Google+

  2. #2
    VBaholic & Loving It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    GetWindowRect()
    Posts
    12,591

    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 09:50 AM. Reason: added bookmark url
    Insomnia is just a byproduct of, "It can't be done"

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {GDI+ Classes/Samples} {Unicode Open/Save Dialog} {Icon Organizer/Extractor}
    {VBA Control Arrays} {XP/Vista Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}
    Share on Google+

  3. #3

    Thread Starter
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Minimum code to draw PNG? GDI+?

    Thanks, LaVolpe. Figured you'd be the one to answer.

    ಠ_ಠ
    Share on Google+

  4. #4
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,928

    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
    The poster formerly known as koolsid
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - Till Date IMP Links : Acceptable Use Policy, FAQ

    MyGear:
    Sony VGN-FZ27G with a triple boot between (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008) and (Win7+Office 2010+VS2010) || Sony VPCCB-45FN with a Win7+Office 2010+VS2010. VM: (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008), (Win8+Office 2010+VS2012) || Mac Book Pro (10.6.8) with Office 2011
    Share on Google+

  5. #5
    VBaholic & Loving It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    GetWindowRect()
    Posts
    12,591

    Re: Minimum code to draw PNG? GDI+?

    Quote Originally Posted by DigiRev View Post
    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
    Insomnia is just a byproduct of, "It can't be done"

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {GDI+ Classes/Samples} {Unicode Open/Save Dialog} {Icon Organizer/Extractor}
    {VBA Control Arrays} {XP/Vista Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}
    Share on Google+

  6. #6
    Hyperactive Member
    Join Date
    Jul 2009
    Posts
    489

    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 ?
    Share on Google+

  7. #7
    VBaholic & Loving It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    GetWindowRect()
    Posts
    12,591

    Re: [RESOLVED] Minimum code to draw PNG? GDI+?

    Quote Originally Posted by whatsup View Post
    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.
    Insomnia is just a byproduct of, "It can't be done"

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {GDI+ Classes/Samples} {Unicode Open/Save Dialog} {Icon Organizer/Extractor}
    {VBA Control Arrays} {XP/Vista Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}
    Share on Google+

  8. #8
    New Member
    Join Date
    Jun 2010
    Posts
    3

    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?
    Share on Google+

  9. #9
    Junior Member
    Join Date
    Apr 2002
    Location
    Alkmaar, the Netherlands
    Posts
    25

    Re: [RESOLVED] Minimum code to draw PNG? GDI+?

    Thanks!!
    Women forgive but never forget. Men forget but never forgive.
    Share on Google+

  10. #10
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    3,624

    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.
    When one of my minions says, "Hey, he's just one guy, what can he do?" I say "This"... and shoot them.

    The problem with putting your lair in a volcano is keeping your robot army from melting.

    I know that the human being and the fish can coexist peacefully - George Bush
    Share on Google+

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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.