no more artifacts
Posted on 4/30/2008 8:16:27 PM
Comment Count ( 0 ) Today I fixed something that was bothering for quite some time. The default thumbnail generation utility within asp.net has left many developers wanting more. This utility produced image thumbnails that had obvious artifacts and quality degradation. I used the quick solution to generate the thumbnail images until I get a couple hours of time where I could sit down and figure out how to manually generate an image. Of course I could talk about image quality until my c code compiles, but words about imagery are empty without ... Yes! Imagery.
Below is the thumbnail generated by the ASP.Net GetThumbnailImage method (1) side-by-side with the "new method" (2) notice the distinct lines in the snowcap and the clarity in the clouds in the second picture vs. the first.
(1)Low Quality
(2)High Quality
The only bad thing is image 2 uses a fairly complicated function and subroutine and image 1 takes only 1 line of code to generate:
thumbnailImage = originalImage.GetThumbnailImage(thumbWidth, thumbHeight, dummyCallBack, New IntPtr())
Of course this article would be pretty useless if I didn't show you how to do it. Plus I'm sure I'd get threatening comments from my google clickthrough people telling me to quit my profession as a programmer. Truth is that I used this article to guide me through the steps. The only difference is that I use decision logic instead of relying on querystring to determine the scaled width or height of the image depending if the image is landscape or portrait oriented.
If (originalImage.Height > originalImage.Width) Then 'portrait
thumbHeight = 150
intShrinkPercent = (thumbHeight / originalImage.Height) * 100
thumbWidth = (intShrinkPercent / 100) * originalImage.Width
ElseIf (originalImage.Height < originalImage.Width) Then 'landscape
thumbHeight = 150
intShrinkPercent = (thumbHeight / originalImage.Height) * 100
thumbWidth = (intShrinkPercent / 100) * originalImage.Width
ElseIf (originalImage.Height = originalImage.Width) Then 'square
thumbHeight = 150
intShrinkPercent = (thumbHeight / originalImage.Height) * 100
thumbWidth = (intShrinkPercent / 100) * originalImage.Width
End If
The other thing the code in the link above failed to do to my liking is it displays the image using the response object. This means the user uploading would have to manually save and transfer the image file to the server. I needed this to save the image automatically so I substituted the following code:
thumbnailImage.Save(Response.OutputStream, codecEncoder, encodeParams)
for this
thumbnailImage.Save((Server.MapPath("./media/") + strThumbnailFileName), codecEncoder, encodeParams)
Instead of Response.OutputStream I use a server path to save the thumbnail (make sure you give the aspnet user access to write to the image folder on your web server.) One neat thing about this code is that it taught me how to use System.graphics to merge words into a .gif image. (This is generated on error) I'm thinking this is a good start on me making my own captcha control.
If you've stumbled upon this article and still need some help, shoot me an email by clicking the "email me" link at the top of the left sidebar or leave an article comment. I can provide the code in it's entirety.