kelly mort
Well-known Member
- Joined
- Apr 10, 2017
- Messages
- 2,169
- Office Version
- 2016
- Platform
- Windows
Hello,
So I have been trying to find a solution to my image compression issue for a while now and I came across this code in vb. Net
I have no idea how to understand it. But I have the feeling it's closer to my need.
I have a folder which contains these images of various sizes that I want to reduce their sizes. As low as 30kb to 50kb is okay. I am not interested in the quality here if that will be an obstacle.
So that when I run the code, then we look into that folder and locate all images above the 50kb then re-save them to that 50kb.
Yeah that's what I can think of now.
If there is a better way to achieve this please highlight that for me.
Doing it manually will not be an option here since there may be few images involved. Say 700
So I have been trying to find a solution to my image compression issue for a while now and I came across this code in vb. Net
I have no idea how to understand it. But I have the feeling it's closer to my need.
I have a folder which contains these images of various sizes that I want to reduce their sizes. As low as 30kb to 50kb is okay. I am not interested in the quality here if that will be an obstacle.
So that when I run the code, then we look into that folder and locate all images above the 50kb then re-save them to that 50kb.
Yeah that's what I can think of now.
If there is a better way to achieve this please highlight that for me.
Doing it manually will not be an option here since there may be few images involved. Say 700
Code:
Code 1 - a given sample
Code:
[COLOR=#0000FF]Private[/COLOR] [COLOR=#0000FF]Class[/COLOR] JpegTools
[COLOR=#0000FF]Private[/COLOR] codecs [COLOR=#0000FF]As[/COLOR] ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()
[COLOR=#0000FF]Private[/COLOR] quality [COLOR=#0000FF]As[/COLOR] [COLOR=#0000FF]Long[/COLOR]
[COLOR=#0000FF]Public[/COLOR] ici [COLOR=#0000FF]As[/COLOR] ImageCodecInfo = [COLOR=#0000FF]Nothing[/COLOR]
[COLOR=#0000FF]Public[/COLOR] ep [COLOR=#0000FF]As[/COLOR] [COLOR=#0000FF]New[/COLOR] EncoderParameters()
[COLOR=#0000FF]Public[/COLOR] compressionRatio [COLOR=#0000FF]As[/COLOR] [COLOR=#0000FF]Long[/COLOR]
[COLOR=#0000FF]Public[/COLOR] [COLOR=#0000FF]Sub[/COLOR] [COLOR=#0000FF]new[/COLOR]([COLOR=#0000FF]ByVal[/COLOR] _compressionRatio [COLOR=#0000FF]As[/COLOR] [COLOR=#0000FF]Long[/COLOR], [COLOR=#0000FF]Optional[/COLOR] [COLOR=#0000FF]ByRef[/COLOR] errMsg [COLOR=#0000FF]As[/COLOR] [COLOR=#0000FF]String[/COLOR] = [COLOR=#800080]"[/COLOR][COLOR=#800080]"[/COLOR])
compressionRatio = _compressionRatio
[COLOR=#0000FF]If[/COLOR] compressionRatio < [COLOR=#000080]0[/COLOR] [COLOR=#0000FF]then[/COLOR] compressionRatio = [COLOR=#000080]0[/COLOR]
[COLOR=#0000FF]If[/COLOR] compressionRatio > [COLOR=#000080]100[/COLOR] [COLOR=#0000FF]then[/COLOR] compressionRatio = [COLOR=#000080]100[/COLOR]
quality = ([COLOR=#000080]100[/COLOR] - compressionRatio)
[COLOR=#0000FF]Try[/COLOR]
[COLOR=#0000FF]For[/COLOR] [COLOR=#0000FF]Each[/COLOR] codec [COLOR=#0000FF]As[/COLOR] ImageCodecInfo [COLOR=#0000FF]In[/COLOR] codecs
[COLOR=#0000FF]If[/COLOR] codec.MimeType = [COLOR=#800080]"[/COLOR][COLOR=#800080]image/jpeg"[/COLOR] [COLOR=#0000FF]Then[/COLOR]
ici = codec
[COLOR=#0000FF]End[/COLOR] [COLOR=#0000FF]If[/COLOR]
[COLOR=#0000FF]Next[/COLOR]
ep.Param([COLOR=#000080]0[/COLOR]) = [COLOR=#0000FF]New[/COLOR] EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality)
[COLOR=#0000FF]Catch[/COLOR] ex [COLOR=#0000FF]As[/COLOR] Exception
errMsg = ex.Message
[COLOR=#0000FF]End[/COLOR] [COLOR=#0000FF]Try[/COLOR]
[COLOR=#0000FF]End[/COLOR] [COLOR=#0000FF]Sub[/COLOR]
[COLOR=#0000FF]End[/COLOR] [COLOR=#0000FF]Class[/COLOR]
[COLOR=#0000FF]Private[/COLOR] JpgTools [COLOR=#0000FF]As[/COLOR] JpegTools
[COLOR=#008000][I]'[/I][/COLOR][COLOR=#008000][I] Save an Image() to a jpeg file and specify the compression % (Valid values for compressionRatio are 0 - 100)[/I][/COLOR]
[COLOR=#0000FF]Public[/COLOR] [COLOR=#0000FF]Function[/COLOR] SaveImgToFile([COLOR=#0000FF]ByRef[/COLOR] img [COLOR=#0000FF]As[/COLOR] Image, [COLOR=#0000FF]ByVal[/COLOR] fullPathWithFileName [COLOR=#0000FF]As[/COLOR] [COLOR=#0000FF]String[/COLOR], [COLOR=#0000FF]ByVal[/COLOR] compressionRatio [COLOR=#0000FF]As[/COLOR] [COLOR=#0000FF]Long[/COLOR], _
[COLOR=#0000FF]Optional[/COLOR] [COLOR=#0000FF]ByRef[/COLOR] errMsg [COLOR=#0000FF]As[/COLOR] [COLOR=#0000FF]String[/COLOR] = [COLOR=#800080]"[/COLOR][COLOR=#800080]"[/COLOR]) [COLOR=#0000FF]As[/COLOR] [COLOR=#0000FF]Boolean[/COLOR]
[COLOR=#0000FF]If[/COLOR] JpgTools [COLOR=#0000FF]Is[/COLOR] [COLOR=#0000FF]Nothing[/COLOR] [COLOR=#0000FF]Then[/COLOR] JpgTools = [COLOR=#0000FF]New[/COLOR] JpegTools(compressionRatio, errMsg)
[COLOR=#0000FF]If[/COLOR] JpgTools.compressionRatio <> compressionRatio [COLOR=#0000FF]then[/COLOR] JpgTools = [COLOR=#0000FF]New[/COLOR] JpegTools(compressionRatio, errMsg)
[COLOR=#0000FF]If[/COLOR] errMsg <> [COLOR=#800080]"[/COLOR][COLOR=#800080]"[/COLOR] [COLOR=#0000FF]then[/COLOR] [COLOR=#0000FF]Return[/COLOR] [COLOR=#0000FF]False[/COLOR]
[COLOR=#0000FF]Try[/COLOR]
img.Save(fullPathWithFileName, JpgTools.ici, JpgTools.ep)
[COLOR=#0000FF]Catch[/COLOR] ex [COLOR=#0000FF]As[/COLOR] Exception
errMsg = ex.Message
[COLOR=#0000FF]Return[/COLOR] [COLOR=#0000FF]False[/COLOR]
[COLOR=#0000FF]End[/COLOR] [COLOR=#0000FF]Try[/COLOR]
[COLOR=#0000FF]Return[/COLOR] [COLOR=#0000FF]True[/COLOR]
[COLOR=#0000FF]End[/COLOR] [COLOR=#0000FF]Function
[/COLOR]
Code:
Code 2 - a given sample
Code:
[COLOR=#666666][FONT='inherit']Int64 RequiredLength = (Int64) (3.8 * 1024);[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']//EncoderParameter epQuality = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (int)numQual.Value);[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']int val = 50;[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']EncoderParameter epQuality = null;[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']//epQuality = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, val);[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']epQuality = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, val);[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']// Store the quality parameter in the list of encoder parameters[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']EncoderParameters epParameters = new EncoderParameters(1);[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']epParameters.Param[0] = epQuality;[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']// Create a new Image object from the current file[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']Image newImage = null;[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']newImage = Image.FromFile(strFile);[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']// Get the file information again, this time we want to find out the extension[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']FileInfo fiPicture = new FileInfo(strFile);[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']// Save the new file at the selected path with the specified encoder parameters, and reuse the same file name[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']string savepath = "TempResize\\" + fiPicture.Name;[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']newImage.Save(savepath, iciJpegCodec, epParameters);[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']FileInfo ResizedInfo = new FileInfo(savepath);[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']Int64 ResizedInBytes = ResizedInfo.Length;[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']//double somrthing = (3.8 * val) / ResizedInKb;[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']Int64 RequiredVal = (RequiredLength * val) / ResizedInBytes;[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']epQuality = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, RequiredVal);[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']// Create a new Image object from the current file[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']Image ResizedImage = null;[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']ResizedImage = Image.FromFile(strFile);[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']epParameters.Param[0] = epQuality;[/FONT][/COLOR]
[COLOR=#666666][FONT='inherit']ResizedImage.Save("TempResize\\New\\" + fiPicture.Name, iciJpegCodec, epParameters);
[/FONT][/COLOR]
Last edited: