Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

possible to load a .png image in picturebox or image 1

Status
Not open for further replies.

sal21

Programmer
Apr 26, 2004
422
IT
possible to load a .png image in picturebox or image ???
Tks
 
Yes, it is.

I covered this before in this forum, so a search might have turned up the answer, but here it is again (but without any explanation).

Code:
[COLOR=blue]Public Function NewLoadPicture(strPath As String) As StdPicture
    With CreateObject("WIA.ImageFile")
        .LoadFile (strPath)
        Set NewLoadPicture = .FileData.Picture
    End With
End Function[/color]
 
HI strong, but i dont understand how to set the pictubox1?
 
This is a runtime function. You cannot do it at design time. At runtime you use NewLoadPicture just like you use LoadPicture
 
Code:
Option Explicit
Private Sub Form_Load()

'[URL unfurl="true"]https://www.tek-tips.com/viewthread.cfm?qid=1819138[/URL]
Set Picture1.Picture = NewLoadPicture("C:\Lavori_Vb6\BANDIERE\FLAG\country-flags-main\png1000px\ad.png")

End Sub
Public Function NewLoadPicture(strPath As String) As StdPicture
    With CreateObject("WIA.ImageFile")
        .LoadFile (strPath)
        Set NewLoadPicture = .FileData.Picture
    End With
End Function

ops...

i'm stupid...
work now! tks.

but possible to resize the image in picturebox1.

Is very big!

see my result



attached the original image.

 
There are loads of ways ofg resizing the image. It really depends on what your plans for that image are. Do you simply want to display it on a form? Within another activex control? Or ...

Actually forget it. Since we already have WIA in action, and WIA can resize things intelligently, let's just modify the NewLoadPicture function slightly so that you can optionally pass a required width and height

Code:
[COLOR=blue]Public Function NewLoadPicture(strPath As String[b], Optional NewX As Long, Optional NewY As Long[/b]) As StdPicture
    [b]Dim img As Object
    Set img = CreateObject("WIA.ImageFile")[/b]
    With [b]img[/b]
        .LoadFile strPath
        
[COLOR=green][b]        ' In production code we would also want to do some more rigorous checking of
        ' NewX and NewY to ensure legit values[/color]
        If NewX <> 0 And NewY <> 0 Then
            With CreateObject("WIA.ImageProcess")
                [COLOR=green]' Resample/scale to NewX x NewY[/color]
                .Filters.Add .FilterInfos!Scale.FilterID
                With .Filters(1).Properties
                    !PreserveAspectRatio = False
                    !MaximumWidth = NewX
                    !MaximumHeight = NewY
                End With
                Set img = .Apply(img)
            End With
        End If[/b]
          
        Set NewLoadPicture = [b]img[/b].FileData.Picture
    End With
End Function
[/color]
 
tks strongm.
curiosity...
i can use your function also for .svg image?
 
How hard is it to try and find out [ponder]

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top