Results 1 to 9 of 9

Thread: convert png to jpg and saved it in c:\mydir\

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,934

    convert png to jpg and saved it in c:\mydir\

    how to convert a png image to jpg image and saved it in c:\mydir1\

    based:
    c:\mydir\img.png

    save as in:
    c:\mydir1\img.jpg


    ???
    tks

  2. #2
    Hyperactive Member -Franky-'s Avatar
    Join Date
    Dec 2022
    Location
    Bremen Germany
    Posts
    481

    Re: convert png to jpg and saved it in c:\mydir\

    The easiest way is to use GDI+. Keep in mind, however, that if the PNG contains (partially) transparent areas, these will be lost when converting to JPG.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,934

    Re: convert png to jpg and saved it in c:\mydir\

    Quote Originally Posted by -Franky- View Post
    The easiest way is to use GDI+. Keep in mind, however, that if the PNG contains (partially) transparent areas, these will be lost when converting to JPG.

    code...

    can you modify with my real path please, tks

  4. #4
    Hyperactive Member -Franky-'s Avatar
    Join Date
    Dec 2022
    Location
    Bremen Germany
    Posts
    481

    Re: convert png to jpg and saved it in c:\mydir\

    Quote Originally Posted by luca90 View Post
    code...
    I think you will certainly find a great many examples of GDI+ here on vbforums.com and be able to adapt them to your needs.

  5. #5
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    6,174

    Re: convert png to jpg and saved it in c:\mydir\

    100% Claude generated code with broken italian prompt like this:

    IN vb6 hot convert png to jpg and saved it in c:\mydir. Show complete sample in Form1
    Code:
    Option Explicit
    
    ' ?? GDI+ startup types ????????????????????????????????????????????????????????
    Private Type GdiplusStartupInput
        GdiplusVersion          As Long
        DebugEventCallback      As Long
        SuppressBackgroundThread As Long
        SuppressExternalCodecs  As Long
    End Type
    
    ' ?? CLSID / encoder-parameter types ??????????????????????????????????????????
    Private Type TCLSID
        Data1   As Long
        Data2   As Integer
        Data3   As Integer
        Data4(7) As Byte
    End Type
    
    Private Type TEncoderParameter
        Guid            As TCLSID
        NumberOfValues  As Long
        Type            As Long
        Value           As Long
    End Type
    
    Private Type TEncoderParameters
        Count           As Long
        Parameter(0)    As TEncoderParameter   ' single-element array
    End Type
    
    ' ?? GDI+ API ?????????????????????????????????????????????????????????????????
    Private Declare Function GdiplusStartup Lib "gdiplus" ( _
        token As Long, inputbuf As GdiplusStartupInput, ByVal outputbuf As Long) As Long
    Private Declare Sub GdiplusShutdown Lib "gdiplus" (ByVal token As Long)
    Private Declare Function GdipLoadImageFromFile Lib "gdiplus" ( _
        ByVal sFilename As Long, image As Long) As Long
    Private Declare Function GdipSaveImageToFile Lib "gdiplus" ( _
        ByVal image As Long, ByVal sFilename As Long, _
        clsid As TCLSID, encoderParams As Any) As Long
    Private Declare Function GdipDisposeImage Lib "gdiplus" (ByVal image As Long) As Long
    
    ' ?? OLE helper (parses CLSID string) ?????????????????????????????????????????
    Private Declare Function CLSIDFromString Lib "ole32" ( _
        ByVal str As Long, clsid As TCLSID) As Long
    
    ' ?? module-level token ????????????????????????????????????????????????????????
    Private mlngToken As Long
    
    ' =============================================================================
    Private Sub Form_Load()
        Dim tSI As GdiplusStartupInput
        tSI.GdiplusVersion = 1
        Call GdiplusStartup(mlngToken, tSI, 0)
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        Call GdiplusShutdown(mlngToken)
    End Sub
    
    Private Sub Command1_Click()
        Call ConvertPngToJpg("C:\mydir\input.png", "C:\mydir\output.jpg", 85)
    End Sub
    
    ' =============================================================================
    ' Converts sSrc (PNG or any GDI+-supported format) to JPEG at lQuality (0-100)
    ' =============================================================================
    Private Sub ConvertPngToJpg(ByVal sSrc As String, ByVal sDst As String, ByVal lQuality As Long)
        Dim lImage          As Long
        Dim lStatus         As Long
        Dim tJpegClsid      As TCLSID
        Dim tParams         As TEncoderParameters
        Dim lQualVal        As Long
    
        ' Load source image
        lStatus = GdipLoadImageFromFile(StrPtr(sSrc), lImage)
        If lStatus <> 0 Then
            MsgBox "Load failed (GDI+ status " & lStatus & "):" & vbLf & sSrc, vbCritical
            Exit Sub
        End If
    
        ' JPEG encoder CLSID  {557CF401-1A04-11D3-9A73-0000F81EF32E}
        Call CLSIDFromString(StrPtr("{557CF401-1A04-11D3-9A73-0000F81EF32E}"), tJpegClsid)
    
        ' Quality parameter  GUID {1D5BE4B5-FA4A-452D-9CDD-5DB35105E7EB}
        lQualVal = lQuality
        tParams.Count = 1
        Call CLSIDFromString(StrPtr("{1D5BE4B5-FA4A-452D-9CDD-5DB35105E7EB}"), _
                             tParams.Parameter(0).Guid)
        tParams.Parameter(0).NumberOfValues = 1
        tParams.Parameter(0).Type = 4          ' EncoderParameterValueTypeLong
        tParams.Parameter(0).Value = VarPtr(lQualVal)
    
        ' Save as JPEG
        lStatus = GdipSaveImageToFile(lImage, StrPtr(sDst), tJpegClsid, tParams)
        If lStatus <> 0 Then
            MsgBox "Save failed (GDI+ status " & lStatus & "):" & vbLf & sDst, vbCritical
        Else
            MsgBox "Saved: " & sDst, vbInformation
        End If
    
        Call GdipDisposeImage(lImage)
    End Sub
    cheers,
    </wqw>

  6. #6

  7. #7
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,658

    Re: convert png to jpg and saved it in c:\mydir\

    Easiest way is the system ImageTranscode object (ITranscodeImage),

    Code:
    Public Function WinTranscode(sSrc As String, sDest As String, Optional lMaxWidth As Long = 0, Optional lMaxHeight As Long = 0) As Long
    Dim pTI As ImageTranscode
    Set pTI = New ImageTranscode
    Dim pStrm As IStream
    Dim psi As IShellItem
    Dim pwi As Long, phg As Long
    SHCreateItemFromParsingName StrPtr(sSrc), Nothing, IID_IShellItem, psi
    Call SHCreateStreamOnFileEx(StrPtr(sDest), STGM_CREATE Or STGM_READWRITE, FILE_ATTRIBUTE_NORMAL, 1, Nothing, pStrm)
    
    WinTranscode = pTI.TranscodeImage(psi, lMaxWidth, lMaxHeight, TI_JPEG, pStrm, pwi, phg)
    pStrm.Commit STGC_DEFAULT
    End Function
    Uses oleexp.tlb/mIID.bas or WDL. No I'm not going to make it easier than adding those.
    Last edited by fafalone; Yesterday at 06:35 PM.

  8. #8
    The Idiot
    Join Date
    Dec 2014
    Posts
    3,003

    Re: convert png to jpg and saved it in c:\mydir\

    I think we can use WIA2 as well (Windows Image Acquisition)
    Dilettante showed it a couple of years ago. easy to use. just do a search. theres plenty of examples.

    Code:
    Public Sub ConvertPngToJpg(ByVal SourcePng As String, ByVal DestJpg As String, Optional ByVal Quality As Long = 85)
        Dim img As Object   ' WIA.ImageFile
        Dim ip As Object    ' WIA.ImageProcess
    
        Set img = CreateObject("WIA.ImageFile")
        img.LoadFile SourcePng
        
        Set ip = CreateObject("WIA.ImageProcess")
        
        ' Add Convert filter
        ip.Filters.Add ip.FilterInfos("Convert").FilterID
        
        ' Set output format to JPEG
        ip.Filters(1).Properties("FormatID").Value = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}"  ' wiaFormatJPEG
        
        ' Optional: Quality (1-100)
        ip.Filters(1).Properties("Quality").Value = Quality
        
        ' Apply conversion
        Set img = ip.Apply(img)
        
        ' Save as JPG
        img.SaveFile DestJpg
    End Sub
    Last edited by baka; Yesterday at 10:08 PM.

  9. #9
    Hyperactive Member -Franky-'s Avatar
    Join Date
    Dec 2022
    Location
    Bremen Germany
    Posts
    481

    Re: convert png to jpg and saved it in c:\mydir\

    We’ve pretty much covered all the available conversion options now; the only thing missing is an example using WIC and WinRT.

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