PDA

View Full Version : Creating Thumbnails


TRPlace
07-15-2008, 01:16 AM
Hey everyone. I am trying to create thumbnails of original images. So far I have this code...

<%@ Page Language="vb" Debug="true" %>
<%

' initialize objects
Dim strFilename as string

Dim g as System.Drawing.Image

Dim newWidth, newHeight, sizer As Integer

Dim boxWidth=100

Dim boxHeight=100

‘ set the filename
strFilename = Server.MapPath("./images/07005.jpg")

‘ create a new image from file
g = System.Drawing.Image.FromFile(strFilename)

If g.height > g.width Then ‘ portrait

sizer = boxWidth / g.height

Else

sizer = boxHeight / g.width

End If

newWidth=CInt(g.width * sizer)

newHeight=CInt(g.height * sizer)

dim g2 as New System.Drawing.Bitmap(g, newWidth, newHeight)

‘ set the content type
response.contenttype="image/jpeg"

‘ send the image to the viewer
g2.Save(Response.OutputStream, g.RawFormat)

‘ tidy up
g.dispose()

%>

How can I wrap this code in a loop to make thumbnails of the images in the images folder and save thumbnail jpg's in an images/thumb folder?

Thanks for the help!!

demtron
07-25-2008, 04:21 PM
Use the Directory.GetFiles method to return the names of the files in your source directory, then loop through the array that's returned to supply the file names to the Image.FromFile method.

I created a component that I use on many websites I've developed. This article at DevX (http://www.devx.com/dotnet/Article/18163/1954) was very useful.

Hope that helps!