JPG Processing in .NET

  • Thread starter Thread starter Samuel
  • Start date Start date
S

Samuel

I shrink pictures in .NET and the quality of the JPG is very low.

Is there anything to do about that?

Thank you,
Samuel
 
Samuel said:
I shrink pictures in .NET and the quality of the JPG is very low.

Is there anything to do about that?

A couple of points:

1) Use a pixel format with an alpha channel when resizing, or else you may
get grey lines at the image borders:

Dim bmp2 As New Bitmap(CInt(targetW), CInt(targetH),
PixelFormat.Format32bppArgb)
Dim g As Graphics = Graphics.FromImage(bmp2)
' some experimentation may be needed with the InterpolationMode
g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic


2) Use the Image.Save(String, ImageCodecInfo, EncoderParameters) overload,
which allows you to set the "quality" used when saving a jfif; to get the
last two parameters:

Dim jpegEncoder As ImageCodecInfo = GetEncoder(ImageFormat.Jpeg)
Dim myEncoderParameters As New EncoderParameters(1)
myEncoderParameters.Param(0) = New
EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 85L)


''' <summary>
''' Return an ImageEncoder given an ImageFormat.
''' </summary>
''' <param name="format">An ImageFormat.</param>
''' <returns>An ImageEncoder.</returns>
''' <remarks></remarks>
Private Function GetEncoder(ByVal format As ImageFormat) As ImageCodecInfo
For Each codec As ImageCodecInfo In ImageCodecInfo.GetImageEncoders()
If codec.FormatID = format.Guid Then
Return codec
End If
Next codec
Return Nothing
End Function


HTH,

Andrew
 
Significant improvement.

Thank you


Andrew Morton said:
A couple of points:

1) Use a pixel format with an alpha channel when resizing, or else you may
get grey lines at the image borders:

Dim bmp2 As New Bitmap(CInt(targetW), CInt(targetH),
PixelFormat.Format32bppArgb)
Dim g As Graphics = Graphics.FromImage(bmp2)
' some experimentation may be needed with the InterpolationMode
g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic


2) Use the Image.Save(String, ImageCodecInfo, EncoderParameters) overload,
which allows you to set the "quality" used when saving a jfif; to get the
last two parameters:

Dim jpegEncoder As ImageCodecInfo = GetEncoder(ImageFormat.Jpeg)
Dim myEncoderParameters As New EncoderParameters(1)
myEncoderParameters.Param(0) = New
EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 85L)


''' <summary>
''' Return an ImageEncoder given an ImageFormat.
''' </summary>
''' <param name="format">An ImageFormat.</param>
''' <returns>An ImageEncoder.</returns>
''' <remarks></remarks>
Private Function GetEncoder(ByVal format As ImageFormat) As ImageCodecInfo
For Each codec As ImageCodecInfo In ImageCodecInfo.GetImageEncoders()
If codec.FormatID = format.Guid Then
Return codec
End If
Next codec
Return Nothing
End Function


HTH,

Andrew
 
Back
Top