Thumbnails in good quality

I

IkBenHet

Hello,

I want to make thumbnails from images. I found this code and modified
it and it works fine:

<%@Import Namespace="System.Drawing.Imaging" %>
<script language="VB" runat="server">
Function ThumbnailCallback() as Boolean
Return False
End Function


Sub Page_Load(sender as Object, e as EventArgs)

'Read in the image filename to create a thumbnail of
Dim imageUrl as String = Request.QueryString("img")

'Read in the width and height
Dim imageHeight as Integer = Request.QueryString("h")
Dim imageWidth as Integer = Request.QueryString("w")

'Add on the appropriate directory
imageUrl = "/upload/" & imageUrl

Dim fullSizeImg as System.Drawing.Image
fullSizeImg =
System.Drawing.Image.FromFile(Server.MapPath(imageUrl))

'Do we need to create a thumbnail?
Response.ContentType = "image/gif"
If imageHeight > 0 and imageWidth > 0 then
Dim dummyCallBack as System.Drawing.Image.GetThumbNailImageAbort
dummyCallBack = New _
System.Drawing.Image.GetThumbnailImageAbort(AddressOf
ThumbnailCallback)

Dim thumbNailImg as System.Drawing.Image
thumbNailImg = fullSizeImg.GetThumbnailImage(imageWidth,
imageHeight, _
dummyCallBack,
IntPtr.Zero)

thumbNailImg.Save(Response.OutputStream, ImageFormat.Gif)
Else
fullSizeImg.Save(Response.OutputStream, ImageFormat.Gif)
End If

End Sub
</script>

The quality of that generated thumbnails is really bad. I found several
posts on it saying that it can be a problem when doing it with images
coming from Digitalcamera... but this is not the case.

I want to create thumbnails with a better quality.

Then I found that by using 'InterpolationMode' you can get a higher
quality image. But I cannot get it working.

The last try was with the below script:

<%@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)
Dim imageUrl as String
ScaleStatic("/test.jpg", 50)
End Sub

Sub ScaleStatic(ByVal FromFile, ByVal intSize)
Dim imgPhoto As System.Drawing.image =
System.Drawing.Image.FromFile(Server.MapPath("/test.jpg"))
Dim sourceWidth As Integer = imgPhoto.Width
Dim sourceHeight As Integer = imgPhoto.Height
Dim sourceX As Integer = 0
Dim sourceY As Integer = 0
Dim destX As Integer = 0
Dim destY As Integer = 0
Dim destWidth As Integer = 0
Dim destHeight As Integer = 0

If imgPhoto.Width > imgPhoto.Height Then
destHeight = ((intSize * 1.0 / sourceWidth) * sourceHeight)
destWidth = intSize
Else
destHeight = intSize
destWidth = ((intSize * 1.0 / sourceHeight) * sourceWidth)
End If

Response.ContentType = "image/jpeg"
Dim bmPhoto As System.Drawing.Bitmap
bmPhoto = New System.Drawing.Bitmap(destWidth, destHeight,
System.Drawing.Imaging.PixelFormat.Format24bppRgb)
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
imgPhoto.VerticalResolution)

Dim grPhoto As System.Drawing.Graphics
grPhoto = System.Drawing.Graphics.FromImage(bmPhoto)
grPhoto.CompositingQuality =
Drawing.Drawing2D.CompositingQuality.HighQuality
grPhoto.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality
grPhoto.InterpolationMode =
Drawing.Drawing2D.InterpolationMode.HighQualityBicubic

grPhoto = imgPhoto.GetThumbnailImage(imgPhoto, New
System.Drawing.Rectangle(destX, destY, destWidth, destHeight), New
System.Drawing.Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
System.Drawing.GraphicsUnit.Pixel)

bmPhoto.Save(Response.OutputStream, ImageFormat.Jpeg)

grPhoto.Dispose()
bmPhoto.Dispose()

End Sub
</script>

But is doesn't work.

Can somebody help me to get the first code sample modified so I returns
a better quality image.

Thanks!
 
K

Kevin Spencer

Hey IkBenHet,

Wish I had the time to explain this, but I'm up against a SERIOUS deadline,
so I'll just give you the following code from an app I wrote, and let you
figure it out. Note: "ImageObject" is an in-memory Bitmap class:

/// <summary>

/// Write Image to browser

/// </summary>

public virtual void WriteToBrowser()

{

HttpResponse Response;

MemoryStream objStream = new MemoryStream();

ImageCodecInfo objImageCodecInfo;

EncoderParameters objEncoderParameters;

try

{

if (_ImageObject == null)

throw new Exception("ImageObject is not initialized. Use CreateImage() to
initialize ImageObject");

if (HttpContext.Current == null)

throw new Exception("No HttpContext");

Response = HttpContext.Current.Response;

switch (_ImageType)

{

default:

objImageCodecInfo = GetEncoderInfo("image/jpeg");

Response.ContentType = "image/jpeg";

break;

case ImageTypesEnum.GIF:

objImageCodecInfo = GetEncoderInfo("image/gif");

Response.ContentType = "image/gif";

break;

case ImageTypesEnum.BMP_TRANSP:

case ImageTypesEnum.BMP:

objImageCodecInfo = GetEncoderInfo("image/bmp");

Response.ContentType = "image/bmp";

break;

}

objEncoderParameters = new EncoderParameters(3);

objEncoderParameters.Param[0] = new EncoderParameter(Encoder.Compression,
(long)EncoderValue.CompressionLZW);

objEncoderParameters.Param[1] = new EncoderParameter(Encoder.Quality,
(long)_QualityPercentage);

objEncoderParameters.Param[2] = new EncoderParameter(Encoder.ColorDepth,
24L);

_ImageObject.Save(Response.OutputStream, objImageCodecInfo,
objEncoderParameters);

}

catch (Exception E)

{

HandleError(E);

}

}


--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top