Answered by:
Quality improvements for image resizing

Question
-
User1796367390 posted
I have followed most of the common techinques for improving the quality of jpeg images when they are resized. Code is below:
Private Shared Function ResizeImageFile(ByVal imageFile() As Byte, ByVal targetSize As Integer) As Byte() Using oldImage As System.Drawing.Image = System.Drawing.Image.FromStream(New MemoryStream(imageFile)) Dim newSize As Size = CalculateDimensions(oldImage.Size, targetSize) Using newImage As Bitmap = New Bitmap(newSize.Width, newSize.Height, PixelFormat.Format24bppRgb) Using canvas As Graphics = Graphics.FromImage(newImage) canvas.SmoothingMode = SmoothingMode.HighQuality canvas.InterpolationMode = InterpolationMode.HighQualityBicubic canvas.PixelOffsetMode = PixelOffsetMode.HighQuality canvas.CompositingQuality = CompositingQuality.HighQuality canvas.DrawImage(oldImage, New Rectangle(New Point(0, 0), newSize)) Dim info() As ImageCodecInfo = ImageCodecInfo.GetImageEncoders() Dim params As EncoderParameters = New Imaging.EncoderParameters(1) params.Param(0) = New EncoderParameter(Encoder.Quality, 100L) Dim m As New MemoryStream newImage.Save(m, info(1), params) Return m.GetBuffer End Using End Using End Using End Function
Im using a HttpHandler to retrieve the images. Is there anything that can be done when rendering the images to improve the quality. I am using something like below to output the image stream:
' Write image stream to the response stream Dim buffersize As Integer = (1024 * 16) Dim buffer() As Byte = New Byte((buffersize) - 1) {} Dim count As Integer = s.Read(buffer, 0, buffersize) Do While (count > 0) context.Response.OutputStream.Write(buffer, 0, count) count = s.Read(buffer, 0, buffersize) Loop
Failing this, can anyone recommend anything else that can be done to further improve my image resizing method (it's pretty good already but want it to be as good as possible)
Thanks,
BenFriday, January 16, 2009 5:20 AM
Answers
-
User1796367390 posted
Thanks for both of your replies.
Now that I have tested it, I am actually very happy with the code I posted and the images quality is very good.
I am only ever resizing once for an image (since I am storing the binary data in a db) so for me this method works well.
Thanks,
Ben
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, January 20, 2009 4:45 AM
All replies
-
User2045693258 posted
one thing i remember reading a while back is when resizing up, always resize in increments so instead of 100x100 to 200x200, go 100x100 to 120x120 to 140x140, etc. not sure if that helps
Monday, January 19, 2009 8:14 AM -
User494352855 posted
Typically you want to avoid that - It usually worsens the problem. Also, minimize jpeg save/reads - each time you save a jpeg you lose data to the lossy compression algorithm. What situation are you in that requires upscaling? Would a desktop tool such as Genuine Fractals be appropriate?
Also, if you end up doing lots of image resizing, you might give this module a try... It's pretty good. Upscaling can be enabled on it, but you'll get standard GDI+ results. It does, however, provide fantastic, photoshop-quality downsampling. http://nathanaeljones.com/products/asp-net-image-resizer/
Monday, January 19, 2009 8:41 AM -
User1796367390 posted
Thanks for both of your replies.
Now that I have tested it, I am actually very happy with the code I posted and the images quality is very good.
I am only ever resizing once for an image (since I am storing the binary data in a db) so for me this method works well.
Thanks,
Ben
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, January 20, 2009 4:45 AM -
User494352855 posted
Great! You seem to have all of your quality settings correct (the most common mistake devs make is leaving the defaults), so you should be getting top-notch downscaling quality.Tuesday, January 20, 2009 12:54 PM