Tuesday, March 13, 2012

Setting thumbnail height

I need to create thumbnails on the fly from a directory containing various sized images, its easy enough to create the thumbnails themselves using my function:

Public Shared Function GenerateThumbNail(ByVal ImagePath As String, ByVal ImageType As String, ByVal ImageWidth As Integer, ByVal ImageHeight As Integer) ' ***************************************************************************** ' Render a thumbnail image on the fly and return as OutputStream: ' ***************************************************************************** Dim oImage, oThumbnail As Drawing.Image Dim imgContentType, imgImageFormat As String ' check this image request for thumbnail actually exists, if not then return default 'no image' thumb: If File.Exists(ImagePath) Then oImage = oImage.FromFile(ImagePath) Else oImage = oImage.FromFile(System.Web.HttpContext.Current.Server.MapPath("ProdImages/noimage.jpg")) End If oThumbnail = oImage.GetThumbnailImage(ImageWidth, ImageHeight, Nothing, Nothing) Select Case ImageType Case "image/jpeg", "image/pjpeg" System.Web.HttpContext.Current.Response.ContentType = "image/jpeg" oThumbnail.Save(System.Web.HttpContext.Current.Response.OutputStream, ImageFormat.Jpeg) Case "image/gif" System.Web.HttpContext.Current.Response.ContentType = "image/gif" oThumbnail.Save(System.Web.HttpContext.Current.Response.OutputStream, ImageFormat.Gif) Case "image/png" System.Web.HttpContext.Current.Response.ContentType = "image/png" oThumbnail.Save(System.Web.HttpContext.Current.Response.OutputStream, ImageFormat.Png) Case Else ' Unidentified, default to jpeg System.Web.HttpContext.Current.Response.ContentType = "image/jpeg" oThumbnail.Save(System.Web.HttpContext.Current.Response.OutputStream, ImageFormat.Jpeg) End Select End Function

But as you see .GetThumbnailImage requires a height value so say if i set width=75 height=50 then some images will look ok thumbnailed at that but if one of the images in the dir is a 550x100 jpeg then you can imagine that that isnt going to look great when resized to 75x50!

Is there any way to just give it the width property so it'll scale the relevent height accordingly (as a browser does normally) or is there a quick way to get this images dimensions before running the function and run some maths to calculate the height based on scaling the width to 75? (seems very intensive doing that though if theres a lot of pics to display on the page).

How does everyone else handle this? Think i'll just end up tweaking my upload routine to store the image dimensions along with the filename in the DB but am interested to see if other alternatives exist.

Ta.

Take a look at this post. It contains some code that should do as you want it to do:

http://forums.asp.net/thread/1403764.aspx


Ta, you certainly went beyond the call of duty on that thread!

Had a think about it and added the following to my original function instead, works lovely:

Public Shared Function GenerateThumbNail(ByVal ImagePath As String, ByVal ImageType As String, ByVal ImageWidth As Integer, ByVal ImageHeight As Integer) ' ***************************************************************************** ' Render a thumbnail image on the fly and return as OutputStream: ' ***************************************************************************** Dim oImage, oThumbnail As Drawing.Image Dim imgContentType, imgImageFormat As String ' check this image request for thumbnail actually exists, if not then return default 'no image' thumb: If File.Exists(ImagePath) Then oImage = oImage.FromFile(ImagePath) Else oImage = oImage.FromFile(System.Web.HttpContext.Current.Server.MapPath("ProdImages/noimage.jpg")) End If ' Calculate proportions based on target width vs current width and scale height accordingly Dim CurrentWidth, CurrentHeight, WidthDiff, NewHeight As Integer CurrentWidth = oImage.Width currentheight = oImage.Height WidthDiff = CurrentWidth / ImageWidth NewHeight = CurrentHeight / WidthDiff oThumbnail = oImage.GetThumbnailImage(ImageWidth,NewHeight, Nothing, Nothing) Select Case ImageType Case "image/jpeg", "image/pjpeg" System.Web.HttpContext.Current.Response.ContentType = "image/jpeg" oThumbnail.Save(System.Web.HttpContext.Current.Response.OutputStream, ImageFormat.Jpeg) Case "image/gif" System.Web.HttpContext.Current.Response.ContentType = "image/gif" oThumbnail.Save(System.Web.HttpContext.Current.Response.OutputStream, ImageFormat.Gif) Case "image/png" System.Web.HttpContext.Current.Response.ContentType = "image/png" oThumbnail.Save(System.Web.HttpContext.Current.Response.OutputStream, ImageFormat.Png) Case Else ' Unidentified, default to jpeg System.Web.HttpContext.Current.Response.ContentType = "image/jpeg" oThumbnail.Save(System.Web.HttpContext.Current.Response.OutputStream, ImageFormat.Jpeg) End Select End Function

and then changed one bit to this in case of the unlikely event the requested thumb size is bigger than the existing image so it'll upscale it ok:

Jobs a goodun'

 ' Calculate proportions based on target width vs current width and scale height accordingly Dim CurrentWidth, CurrentHeight, WidthDiff, NewHeight As Double CurrentWidth = oImage.Width CurrentHeight = oImage.Height If CurrentWidth > ImageWidth Then WidthDiff = CurrentWidth / ImageWidth NewHeight = CurrentHeight / WidthDiff Else WidthDiff = ImageWidth / CurrentWidth NewHeight = CurrentHeight * WidthDiff End If

0 comments:

Post a Comment