Results 1 to 9 of 9

Thread: [RESOLVED] Again on PNG into PictureBox

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2022
    Posts
    3

    Resolved [RESOLVED] Again on PNG into PictureBox

    Hi, I'm new in this forum, even if I use VB6 since 2010 (possibly).
    I have a minimal need which is to display a PNG image inside a picturebox (o image) resized to the availabe space. I do not care of fancy effect, not even transparency; I just need to select some pictures among a quite large number of images, by type: JPG or PNG. So It is enough for me to SEE the image indipendently from type and streched to the picturebox size.

    I spent hours reading hundreds of posts, particularly from La Volpe (who unfortunately left this forum) but, I''m not a nerd in this area, nor my English knowledge helps.
    I tried different solutions found here, but without success: every time something is missing: libraries, references, components, dll...or even manifests (that I know nothing).

    Does anyone a 'simple' complete solution for my need?

    Note: beside to the above, I found another problem: a number of pictures denominated as JPG which are in reality of different type, PNG in example, so I can not load with the standard picturebox. How can I detect which is the real original format?
    Thanks to all
    Last edited by Sirio; Oct 7th, 2022 at 08:44 AM.

  2. #2
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    8,425

    Re: Again on PNG into PictureBox

    There are several solutions, but here's some code to get a PNG (including alpha if it has it) into an Image control. It also has the ability to resize it as it's pulling it in.

    ADDED: You'll want to focus on the LoadPngIntoPictureWithAlpha call, and the dScalingFactor argument can be used to scale. It always preserves aspect ratio, but that could be changed with minimal work.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  3. #3
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,310

    Re: Again on PNG into PictureBox

    See:
    https://www.vbforums.com/showthread....3-Image-Viewer
    Because the MS viewers are so painfully slow, I have made it my default picture viewer.

    J.A. Coutts

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Posts
    23,803

    Re: Again on PNG into PictureBox

    You can do any scaling needed via PantPicture or Render methods.

    That just leaves loading image files and wrapping the bitmaps as StdPicture/IPicture objects. WIA 2.0 can easily do that.

    Thus we've had a simple solution since at least XP SP2.

    Code:
    Option Explicit
    
    'Reference to: Microsoft Windows Image Acquisition Library v2.0
    
    Private Pic As IPicture
    
    Private Sub Form_Load()
        With New WIA.ImageFile
            .LoadFile "sample.png"
            Set Pic = .FileData.Picture
        End With
    End Sub
    
    Private Sub Form_REsize()
        If WindowState <> vbMinimized Then
            AutoRedraw = True
            Cls
            With Pic
                .Render hDC, _
                        0, _
                        0, _
                        ScaleX(ScaleWidth, ScaleMode, vbPixels), _
                        ScaleY(ScaleHeight, ScaleMode, vbPixels), _
                        0, _
                        .Height, _
                        .Width, _
                        -.Height, _
                        ByVal 0&
            End With
            AutoRedraw = False
        End If
    End Sub
    Last edited by dilettante; Oct 6th, 2022 at 03:30 PM. Reason: Use Resize event, Paint even does not fire when Form is sized smaller!

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    23,803

    Re: Again on PNG into PictureBox

    Quote Originally Posted by Sirio View Post
    How can I detect which is the real original format?
    Code:
        With New WIA.ImageFile
            .LoadFile "sample.xxx"
            Debug.Print .FileExtension
        End With
    Here sample.xxx is really a PNG file. What prints is "png" giving you what you asked for.

    The WIA 2.0 SDK contains (among other items) a useful CHM Help document. No idea where you might obtain it at this very late date though.

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Posts
    23,803

    Re: Again on PNG into PictureBox

    If you merely need to slop the image into a PictureBox, scaling it to the current PictureBox size then:

    Code:
    Option Explicit
    
    Private Sub Form_Load()
        With New WIA.ImageFile
            .LoadFile "sample.png"
            Picture1.AutoRedraw = True
            Picture1.PaintPicture .FileData.Picture(), _
                                  0, _
                                  0, _
                                  Picture1.ScaleWidth, _
                                  Picture1.ScaleHeight
            Picture1.AutoRedraw = False
        End With
    End Sub

  7. #7

    Thread Starter
    New Member
    Join Date
    Oct 2022
    Posts
    3

    Re: Again on PNG into PictureBox

    Thanks 'dilettante', I think those are really good informations. I'll test them all asap.
    Thanks to the others who aswered too.

  8. #8

    Thread Starter
    New Member
    Join Date
    Oct 2022
    Posts
    3

    Re: Again on PNG into PictureBox

    Perfect, exactly was I looking for. I should study WIA (again? after so many years?); seems very poverful, and thought.
    Many thanks

  9. #9
    PowerPoster
    Join Date
    Feb 2006
    Posts
    23,803

    Re: [RESOLVED] Again on PNG into PictureBox

    Most of WIA deals with scanners and still cameras and stuff like JPEG image EXIF data. What's left is mostly thin wrappers around some common GDI+ objects and operations and that's what I used here.

    WIA is just a quicker (less source code) way to make use of some of those operations than writing code to use the GDI+ Flat API from VB6.

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