Print Page | Close Window

Image manipulation

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=12530
Printed Date: 29 March 2026 at 8:42am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Image manipulation
Posted By: Mattblack
Subject: Image manipulation
Date Posted: 13 November 2004 at 7:37pm

hi all, im back again!

How do i create a thumbnail of a picture dynamically?  Do, say i have a directory of images, and each one needs to be displayed as a small thumbnail which can be clicked on to open the full picture.

The thumbnail would need to be small in size aswell as filesize as there will be loads on 1 page.

I dont have access to the server to install scripts.

Thanks




Replies:
Posted By: dpyers
Date Posted: 13 November 2004 at 10:32pm

Well, if you can't install an asp script, you're hosed. There's no way to create thumbnails server-side without scripting and a 3rd party component like aspjpeg.

You can resize the the original image using html height and width, but it still uses the original large file.

Autocreating thumbnails doesn't work very well anyway if the original pics don't have the same hxw ration as the thumbnails. Even then, image quality usually suffers. You're better off to create the thumbnails in some offline paint program if you need quality thumbnails.



-------------

Lead me not into temptation... I know the short cut, follow me.


Posted By: Mart
Date Posted: 14 November 2004 at 4:33am
If you have ASP.NET just save this code as thumbnail.aspx and then just point the image to thumbnail.aspx?source=images/mypicture.jpg


<%@ Page Language="vb" %>
<%@ Import Namespace="System.Drawing" %>
<script runat="server">
Public Sub Page_Load
Response.ContentType = "Image/Jpeg"

Dim SourceImage As New Bitmap(Server.MapPath(Request.QueryString("source"))

Dim NewWidth As Integer = SourceImage.Width
Dim NewHeight As Integer = SourceImage.Height

Do Until NewWidth < 100 And NewHeight < 100
NewWidth = NewWidth / 2
NewHeight = NewHeight / 2
Loop

Dim SourceImage As Bitmap = b.GetThumbnailImage(NewWidth, NewHeight, Nothing, IntPtr.Zero)

Thumbnail.Save(Response.OutputStream, ImageFormat.Jpeg)
Thumbnail.Dispose()
SourceImage.Dispose()
End Sub
</script>



Posted By: Mattblack
Date Posted: 14 November 2004 at 8:10am

hi all, thanks for that.

Dpyers, it must be "on the fly" as these images will be uploaded by visitors (motorbike sales/wanteds)

Mart, thanks for that.  Yes i do have .Net, just dont have experience with it.  Code looks good though, will try it now,

Thanks to you both



Posted By: Mattblack
Date Posted: 14 November 2004 at 8:16am

cant get it to work mate, just get the little red cross in box.

Tried all options with the source, as virtual and physical paths.  In quotes and without.

eeeek!



Posted By: Mart
Date Posted: 14 November 2004 at 8:57am
Ok, go to the image resizer in your browser (i.e. yoursite.com/thumbnail.aspx?source=motorbike1.jpg)

and there should be an error showing, paste it in here


Posted By: Mart
Date Posted: 14 November 2004 at 10:12am
actually I can see the error, this should work:


<%@ Page Language="vb" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<script runat="server">
Public Sub Page_Load
Response.ContentType = "Image/Jpeg"

Dim SourceImage As New Bitmap(Server.MapPath(Request.QueryString("source")))

Dim NewWidth As Integer = SourceImage.Width
Dim NewHeight As Integer = SourceImage.Height

Do Until NewWidth < 100 And NewHeight < 100
NewWidth = NewWidth / 2
NewHeight = NewHeight / 2
Loop

Dim Thumbnail As Bitmap = SourceImage.GetThumbnailImage(NewWidth, NewHeight, Nothing, IntPtr.Zero)

Thumbnail.Save(Response.OutputStream, ImageFormat.Jpeg)
Thumbnail.Dispose()
SourceImage.Dispose()
End Sub
</script>



Posted By: Mart
Date Posted: 14 November 2004 at 10:19am
p.s. that script will scale the image down until it is smaller than 100px by 100px, if you want the thumbnail bigger or smaller just edit line 13:

Do Until NewWidth < [smller than width] And NewHeight < [smaller than height]


Posted By: Mattblack
Date Posted: 14 November 2004 at 11:17am

brilliant!  You're a genious!!!  :-)

Thank you.



Posted By: Mattblack
Date Posted: 14 November 2004 at 11:19am
even makes it a smaller file size!  Im well chuffed now


Posted By: jchonparadise
Date Posted: 21 June 2006 at 10:26pm
I realize this is a really old thread, but I just came upon it.
 
I get the following error when i implement the code:
 

Parameter is not valid.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Parameter is not valid.

Source Error:

Line 6:  Response.ContentType = "Image/Jpeg"
Line 7:  
Line 8:  Dim SourceImage As New Bitmap(Server.MapPath(Request.QueryString("source")))
Line 9:  
Line 10: Dim NewWidth As Integer = SourceImage.Width
 
Stack Trace:

[ArgumentException: Parameter is not valid.]
   System.Drawing.Bitmap..ctor(String filename) +375381
   ASP.thumbnail_aspx.Page_Load() in [REMOVED because I'm paranoid]\thumbnail.aspx:8
   System.Web.Util.CalliHelper.ArglessFunctionCaller(IntPtr fp, Object o) +5
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +781855
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +47
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061

Help?


-------------
<><
Jchon


Posted By: Mart
Date Posted: 26 June 2006 at 7:04pm
Can you post the entire source to thumbnail.aspx, including any modifications?
As far as i can tell the original code that i posted should work fine, also what parameters are you providing the page with?
You have to remember that the source parameter should be a virtual path starting from the directory that the script is in, it's then translated to a physical path.


Posted By: jchonparadise
Date Posted: 26 June 2006 at 7:14pm
Thanks for the help - here's the source (nothing was changed from what you posted):

<%@ Page Language="vb" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<script runat="server">
Public Sub Page_Load
Response.ContentType = "Image/Jpeg"
Dim SourceImage As New Bitmap(Server.MapPath(Request.QueryString("source")))
Dim NewWidth As Integer = SourceImage.Width
Dim NewHeight As Integer = SourceImage.Height
Do Until NewWidth < 100 And NewHeight < 100
NewWidth = NewWidth / 2
NewHeight = NewHeight / 2
Loop
Dim Thumbnail As Bitmap = SourceImage.GetThumbnailImage(NewWidth, NewHeight, Nothing, IntPtr.Zero)
Thumbnail.Save(Response.OutputStream, ImageFormat.Jpeg)
Thumbnail.Dispose()
SourceImage.Dispose()
End Sub
</script>
 
And I did point the source to the virtual path (similar to your example: thumbnail.aspx?source=images/test.jpg)


-------------
<><
Jchon



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net