|
-
Thread Starter
PowerPoster
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
-
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.
-
Thread Starter
PowerPoster
Re: convert png to jpg and saved it in c:\mydir\
 Originally Posted by -Franky-
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
-
Re: convert png to jpg and saved it in c:\mydir\
 Originally Posted by luca90
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.
-
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>
-
Re: convert png to jpg and saved it in c:\mydir\
vbForums Vibe Coding > AI Vibe Coding!
-
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.
-
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.
-
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.
-
Re: convert png to jpg and saved it in c:\mydir\
 Originally Posted by -Franky-
We’ve pretty much covered all the available conversion options now...
Almost! Here's a RC6/Cairo one-liner 
Code:
Cairo.CreateSurface(0,0, ,"c:\mydir\img.png").WriteContentToJpgFile "c:\mydir1\img.jpg"
-
Re: convert png to jpg and saved it in c:\mydir\
Personally, I'm a bit curious about why you'd want to do this. PNG is an is an extremely versatile and general format. I suppose if you're just after absolute compression and small file sizes, and are willing to sacrifice the alpha channel, I can see it.
And Luca, just in case you're unaware, the alpha channel allows PNG files to have transparent (and even translucent) areas anywhere on the image, whereas you can't do that with JPG files. I suppose you can with JP2000 (aka, JP2) files, but that's a whole other can of worms that wasn't discussed in any of the above posts.
Last edited by Elroy; Today at 11:53 AM.
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. To all, peace and happiness.
-
Re: convert png to jpg and saved it in c:\mydir\
Personally I find it faster and easier to use Paint. My PicView program is able to adjust the picture size to match the screen, but sometimes the picture comes in oversized, on its side, and in the wrong format. Paint is able to adjust the size, rotate the picture, and store it in any format you want. That way you still have the original high resolution picture, as well as the one adjusted to your own specs.
J.A. Coutts
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|