Creating Thumbnails RRS feed

  • Question

  • User680026537 posted

    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

    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


    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

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

    ‘ tidy up

    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!!


    Tuesday, July 15, 2008 8:29 PM

All replies

  • User-125547262 posted

     Please see this article from Bertrand who is on the asp.net team


    Wednesday, July 16, 2008 5:15 AM
  • Sunday, March 1, 2009 3:37 AM
  • User-525215917 posted
    Try something like this
    DirectoryInfo dirInfo = new DirectoryInfo(Server.MapPath("~/images/"));
    foreach(FileInfo file in dirInfo.GetFiles())
        //file.FullName gives you full path to file
        //make sure you handle exceptions in the case of invalid images 
        //and files that are not image files
    Sunday, March 1, 2009 9:38 AM