Anyone know of a work around in .Net to retain the highest possible resolution of a photo or image after compression or resizing?

Recommended Answers

All 4 Replies

Use this code:

Dim g As Graphics = Graphics.FromImage(YourBitmap)

g.SmoothingMode = SmoothingMode.HighQuality
g.InterpolationMode = InterpolationMode.HighQualityBicubic
g.PixelOffsetMode = PixelOffsetMode.HighQuality

And when saving use this:

Dim myParams As EncoderParameters = New EncoderParameters(1)
myParams.Param(0) = New EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 80)
YourBitmap.Save(YourPath, GetEncoderInfo(MimeType), myParams)

This is GetEncoderInfo function:

Public Function GetEncoderInfo(ByVal MimeType As String) As ImageCodecInfo
        Dim j As Integer
        Dim encoders As ImageCodecInfo()
        encoders = ImageCodecInfo.GetImageEncoders()
        j = 0
        Do While j < encoders.Length
            If encoders(j).MimeType = MimeType.Trim.Replace("pjpeg", "jpeg") Then
                Return encoders(j)
            End If
            j += 1
        Loop
        Return Nothing
End Function

Wow- that was quick. I am a designer researching this for my programmer -- who is too slammed to research this. We are trying to keep the highest quality for uploaded photos, much like www.flickr.com does for it's members.

When you upload a 4000px by 2000px 5 meg image to (Flickr) it will compress to 1/2 or 2.5 meg and display at the appropriate aspect ratio for their site.

Our .Net app is compressing the photo file sizes much smaller, where as uploading a test photo to Flickr will sace at 58760 Kb our app. will save the same photo at 17240 Kb.

I will share your feedback with him. Thank you for taking the time to reply. It is greatly appreciated.

Rick

ManicCW

Here is the string we are using (C#)

System.Drawing.
Image image1 = System.Drawing.Image.FromFile(Server.MapPath(imgfilepath + actimgFilename));
System.Drawing.
Image smallimage = new Bitmap (imgNewWidth, imgNewHeight, image1.PixelFormat);
Graphics oGraphic = Graphics.FromImage(smallimage);
oGraphic.CompositingQuality =
CompositingQuality.HighQuality;
oGraphic.SmoothingMode =
SmoothingMode.HighQuality;
oGraphic.InterpolationMode =
InterpolationMode.HighQualityBicubic;
oGraphic.PixelOffsetMode =
PixelOffsetMode.HighQuality;
Rectangle oRectange = new Rectangle (0, 0, imgNewWidth, imgNewHeight);
oGraphic.DrawImage(image1, oRectange);
smallimage.Save(Server.MapPath(imgfilepath + newFilename), System.Drawing.Imaging.
ImageFormat.Jpeg);

The only thing that you are missing is EncoderParameters. Use the above code to set quality of saved image.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.