Hi All,
On my webpage i have a .NET script using the GetThumbnailImage function to create jpeg thumbnails, but a known problem with this is the creation of fuzzy images having bad image quality. Here is the piece of code (it receives the image file path, desired width and height from the querystring):
To improve image quality i read something about the parameters SmoothingMode, CompositingQuality and InterpolationMode. Below is a piece of sample code using these parameters:Code:<%@Import Namespace="System.IO" %> <%@Import Namespace="System.Drawing.Imaging" %> <%@Import Namespace="System.Drawing.Drawing2D" %> <script language="vb" runat="server"> Function ThumbnailCallback() as Boolean Return False End Function Sub Page_Load(sender as Object, e as EventArgs) 'Get the image path and maximum width and height from the querystring Dim IMAGE_DIRECTORY as String = Request.QueryString("img") Dim maxWidth as Integer = Request.QueryString("w") Dim maxHeight as Integer = Request.QueryString("h") 'Get information about the original (large) image Dim currentImage as System.Drawing.Image currentImage = System.Drawing.Image.FromFile(Server.MapPath(IMAGE_DIRECTORY)) Dim imgHeight, imgWidth as Integer imgHeight = currentImage.Height imgWidth = currentImage.Width 'Rescale the size of the original width and height If imgWidth > maxWidth OR imgHeight > maxHeight then 'Determine what dimension is off by more Dim deltaWidth as Integer = imgWidth - maxWidth Dim deltaHeight as Integer = imgHeight - maxHeight Dim scaleFactor as Double If deltaHeight > deltaWidth then 'Scale by the height scaleFactor = maxHeight / imgHeight Else 'Scale by the Width scaleFactor = maxWidth / imgWidth End If imgWidth *= scaleFactor imgHeight *= scaleFactor End If 'Set the content type to image/jpeg so the <img> tag can handle it Response.ContentType = "image/jpeg" 'In case the height or width of the original image do not equal the rescaled values If imgHeight <> currentImage.Height Or imgWidth <> currentImage.Width then 'Create useless callback Dim dummyCallBack as System.Drawing.Image.GetThumbNailImageAbort dummyCallBack = New System.Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback) 'Create the thumbnail image (dummycallback uses a fake zeropointer) Dim thumbNailImg as System.Drawing.Image thumbNailImg = currentImage.GetThumbnailImage(imgWidth, imgHeight, dummyCallBack, IntPtr.Zero) 'Stream the thumbnail response towards the <img> tag thumbNailImg.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg) 'Clean up / Dispose... ThumbnailImg.Dispose() Else currentImage.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg) End If 'Clean up by dispose currentImage.Dispose() End Sub </script>
I tried different ways to integrate this approach with my original code but it's just not working. I just want to get rid of the fuzzy thumbnails and create them with good image quality.Code:Bitmap bmp = new Bitmap(imgWidth, imgHeight); System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp); gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality ; gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, imgWidth, imgHeight); gr.DrawImage(currentImage, rectDestination, 0, 0, currentImage.Width, currentImage.Height, GraphicsUnit.Pixel); bmp.Save(path); bmp.Dispose(); currentImage.Dispose();
Does anyone have experience with this? How to combine the 2 pieces of code above to get a script which creates optimal thumbs?