How to save a jpeg image with 50% quality.
Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
5 posts
Page 1 of 1
I know many wonder how those good image editors save images with f.ex 50% quality.
A normal image (in vb) can be saved to a stream or a file. Here is the possible ways to save to a file:
Remember to import the System.Drawing.Imaging namespace
So lets say we have an image called someimage, we want to save this image with 75% quality. We need to get the right image codec, and we need to create the encoder parameters. We could create a method to get the right codec. It would be like this:
We also need to create the encoder parameters. That can be done easy.
A normal image (in vb) can be saved to a stream or a file. Here is the possible ways to save to a file:
Code: Select all
You will be learning about the last one.Image.Save(filename As String)
Image.Save(filename As String, ImageFormat)
Image.Save(filename As String, imageCodec as ImageCodecInfo, encoderParams As EncoderParameters)
Remember to import the System.Drawing.Imaging namespace
So lets say we have an image called someimage, we want to save this image with 75% quality. We need to get the right image codec, and we need to create the encoder parameters. We could create a method to get the right codec. It would be like this:
Code: Select all
The mimetype for JPEG is image/jpeg. Function GetImageCodec(ByVal mimeType As String) As ImageCodecInfo
'Stores the avaliable codec infos in an array
Dim codecs() As ImageCodecInfo = ImageCodecInfo.GetImageEncoders
Dim theRightOne As ImageCodecInfo
For codecIndex = 0 To codecs.Length - 1
'Check if it is the right codec
If codecs(codecIndex).MimeType = mimeType Then
'This matches the codec we want
theRightOne = codecs(codecIndex)
Exit For
End If
Next
'Cleanup
Array.Clear(codecs, 0, codecs.Length)
Return theRightOne
End Function
We also need to create the encoder parameters. That can be done easy.
Code: Select all
Remember that the mimeType for the JPEG codec is image/jpeg. Now we can save our image with this little piece of code:
Dim EncoderParameters As New EncoderParameters(1) 'Store 1 parameter
EncoderParameters.Param(0) = New EncoderParameter(Encoder.Quality, 75) 'Creates an EncoderParameter with the Quality encoder and sets it value to 75 (%)
Code: Select all
Function SaveImageWithQuality(ByVal image As Image, ByVal filename As String, ByVal Quality As Integer)
If Quality < 0 OrElse Quality > 100 Then
Throw New ArgumentOutOfRangeException("The quality must be between 0 and 100")
Exit Function
End If
Dim EncoderParameters As New EncoderParameters(1) 'Store 1 parameter
EncoderParameters.Param(0) = New EncoderParameter(Encoder.Quality, Quality) 'Creates an EncoderParameter with the Quality encoder and sets it value from the 'Quality' parameter
Dim codec As ImageCodecInfo = GetImageCodec("image/jpeg")
image.Save(filename, codec, EncoderParameters) 'Saves the image to 'filename' with the jpeg encoder and the quality from the 'quality' parameter
End Function
Code: Select all
SaveImageWithQuality(someimage, "Test.jpg", 50)
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
![Image]()
![Image]()
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!

thx
though i dont think you can +rep me since you already have

LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
![Image]()
![Image]()
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!

Good work,
i never tought it can be done in this Way.
i never tought it can be done in this Way.
5 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023