csharp tutorialDigital cameras are becoming more and more common. But creating new pages for every single picture that you take can be time consuming to say the least. Here’s how you can make a simple photo album that automatically scans and displays all of the pictures in a specified folder.

Note: The idea for this came from aspalliance.com. I’ve converted the code to C# and added features,comments and instructions.

First we need to import the System.IO namespace. Do so by adding this line to the top of your page.


Now we need to build the unordered list that will display the pictures

    < % /* The Enumerator allows you to loop through the collection object without complicated code */ IDictionaryEnumerator myEnum= pictures.GetEnumerator(); //We'll use this int to control the number of columns int i=1; while(myEnum.MoveNext()){ /* Each picture is given a link that is contained in the Key. This will allow you to click on a pic and get the full size version. The value is a link to our thumbnail generating page which shrinks the image for a thumbnail */ Response.Write("
  • "); Response.Write(""); Response.Write("
  • "); i++; } %>

We now need a page that shrinks our images. All we do is create a page that returns an actual image, we can then reference this page within image tags!

<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Drawing"%>
<%@ Import Namespace="System.Drawing.Imaging"%>

This page serves as the source for the images on our photo album page. It takes the path of the big picture and writes the smaller image to the Response
Output stream. Save this file as getThumb.aspx and place it in the same directory
as your photo album page.

There you have it. No longer do you need to painstakingly add each picture manually to your webpage to amaze your friends. Instead, justdrag and drop the picture into the designated folder and your off. Modifications to allow you to change directories or to recurse through the
given directories will be left for your own imagination.
You could also modify this code to display other file types, the choices are endless!

Enjoy!

Comments are closed.