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 08:14 AM.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    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} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  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.

    ಠ_ಠ

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    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} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  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 ?

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    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} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  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?

  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.

  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
    7,900

    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
  •  



Click Here to Expand Forum to Full Width