Is there an easy way to create a page with thumbnail images of the users without downloading the entire image and then shrinking it in size? Right now in the page displaying user images, even if the picture is set to 100px by 100px, if I right click on the
image, the picture size is about 1 MB. If I could get a smaller image, that would really speed up the page loading.
right now I'm working on a servercontrol to do that, in the meantime, the easy solution is
1. Create an aspx page that accepts url of image and responses in image/jpg (or some other) content Type (see the code below)
2. Reference this page in img's src
<img src="GetThumb.aspx?folder=folder_here&filename=myimage.jpg">
GetThumb,aspx looks like this:
<%
' Initialize objects
Dim objImage, objThumbnail As System.Drawing.Image
Dim strServerPath, strFilename As String
Dim shtWidth, shtHeight,ratio As Short
' Get image folder path on server - use "\" string if root
strServerPath = Server.MapPath("../binaries/Public/" & Request.QueryString("folder") & "/") 'CHANGE THIS AT WILL
' Retrieve name of file to resize from query string
strFilename = strServerPath & Request.QueryString("filename")
' Retrieve file, or error.gif if not available
Try
objImage = objImage.FromFile(strFilename)
Catch
objImage = objImage.FromFile(strServerPath & "error.gif")
End Try
' Retrieve width from query string
If Request.QueryString("width") = Nothing Then
shtWidth = objImage.Width
ElseIf Request.QueryString("width") < 1 Then
shtWidth = 100
Else
shtWidth = Request.QueryString("width")
End If
shtWidth = 100
' Work out a proportionate height from width
'shtHeight = objImage.Height / (objImage.Width / shtWidth)
'shtHeight = (shtWidth * objImage.Height) / objImage.Width
if objImage.Height > objImage.Width then
ratio = objImage.Height * 100 / objImage.Width
shtHeight = 100
shtWidth = shtHeight * 100 / ratio
elseif objImage.Height <= objImage.Width then
ratio = objImage.Width * 100 / objImage.Height
shtWidth = 100
shtHeight = shtWidth * 100 / ratio
end if
' Create thumbnail
objThumbnail = objImage.GetThumbnailImage(shtWidth, _
shtHeight, Nothing, System.IntPtr.Zero)
' Send down to client
Response.ContentType = "image/jpeg"
objThumbnail.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
' Tidy up
objImage.Dispose()
objThumbnail.Dispose()
dilbert1947
Member
262 Points
171 Posts
thumbnail images?
Feb 19, 2007 08:14 PM|LINK
DarrellNorton
All-Star
78896 Points
8717 Posts
Moderator
MVP
Re: thumbnail images?
Feb 19, 2007 08:32 PM|LINK
Check out this article:
True Image Resizing with ASP.NET
Darrell Norton's Blog
Please click "Mark as Answer" if this helped you.
Mauro_net
Contributor
2114 Points
399 Posts
Re: thumbnail images?
Feb 20, 2007 07:26 AM|LINK
Hi,
right now I'm working on a servercontrol to do that, in the meantime, the easy solution is
1. Create an aspx page that accepts url of image and responses in image/jpg (or some other) content Type (see the code below)
2. Reference this page in img's src
<img src="GetThumb.aspx?folder=folder_here&filename=myimage.jpg">
GetThumb,aspx looks like this:
<%
' Initialize objects
Dim objImage, objThumbnail As System.Drawing.Image
Dim strServerPath, strFilename As String
Dim shtWidth, shtHeight,ratio As Short
' Get image folder path on server - use "\" string if root
strServerPath = Server.MapPath("../binaries/Public/" & Request.QueryString("folder") & "/") 'CHANGE THIS AT WILL
' Retrieve name of file to resize from query string
strFilename = strServerPath & Request.QueryString("filename")
' Retrieve file, or error.gif if not available
Try
objImage = objImage.FromFile(strFilename)
Catch
objImage = objImage.FromFile(strServerPath & "error.gif")
End Try
' Retrieve width from query string
If Request.QueryString("width") = Nothing Then
shtWidth = objImage.Width
ElseIf Request.QueryString("width") < 1 Then
shtWidth = 100
Else
shtWidth = Request.QueryString("width")
End If
shtWidth = 100
' Work out a proportionate height from width
'shtHeight = objImage.Height / (objImage.Width / shtWidth)
'shtHeight = (shtWidth * objImage.Height) / objImage.Width
if objImage.Height > objImage.Width then
ratio = objImage.Height * 100 / objImage.Width
shtHeight = 100
shtWidth = shtHeight * 100 / ratio
elseif objImage.Height <= objImage.Width then
ratio = objImage.Width * 100 / objImage.Height
shtWidth = 100
shtHeight = shtWidth * 100 / ratio
end if
' Create thumbnail
objThumbnail = objImage.GetThumbnailImage(shtWidth, _
shtHeight, Nothing, System.IntPtr.Zero)
' Send down to client
Response.ContentType = "image/jpeg"
objThumbnail.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
' Tidy up
objImage.Dispose()
objThumbnail.Dispose()
%>
hope this helps
fozia_izhar
Member
95 Points
82 Posts
Re: thumbnail images?
Feb 20, 2007 07:38 AM|LINK
Check out following link,it may be helpful to u.
http://west-wind.com/weblog/posts/283.aspx
Fozia