Hey All,
I have 5 pictures(.jpeg) inside one folder...How do I make all those images get displayed in my .aspx page(like a thumbnail?)?
Can I create objects of Image class inside a for loop and make it display? But in anycase I don't know how to make it display... Should I create a table and make the images to display in each cell??
Any help would be appreciated..

Thanks
Ani

Dani AI

Generated

wanted a simple way to show all JPEGs from a folder on an ASPX page. mentioned thumbnail generation and recommended a lighter data-bound approach. A concise, practical pattern is: enumerate files in the folder, build a List<string> of virtual image URLs, and bind that list to a Repeater (no database required).

Example code-behind (run on Page_Load when not IsPostBack):

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string folderVirtual = "~/images/gallery";
        string folderPath = Server.MapPath(folderVirtual);
        var images = new List<string>();

        if (Directory.Exists(folderPath))
        {
            var files = Directory.GetFiles(folderPath)
                .Where(f => f.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase)
                         || f.EndsWith(".jpeg", StringComparison.OrdinalIgnoreCase));

            foreach (var file in files)
                images.Add(VirtualPathUtility.ToAbsolute(folderVirtual + "/" + Path.GetFileName(file)));
        }

        rptImages.DataSource = images;
        rptImages.DataBind();
    }
}

Minimal Repeater markup for thumbnails (client-side sizing or pre-made smaller files recommended):

<asp:Repeater ID="rptImages" runat="server">
  <ItemTemplate>
    <img src='<%# Container.DataItem %>' alt='' style='width:120px;height:auto;margin:4px;' />
  </ItemTemplate>
</asp:Repeater>

Notes and cautions: DataSource here is any IEnumerable (List<string> is fine) — not a SQL DB. Prefer server-generated thumbnails (saved to a thumbs folder) for bandwidth and load-time savings; CSS-only shrinking still downloads full images. Always validate file extensions, avoid accepting arbitrary path input (prevent traversal), and ensure proper file permissions. For larger galleries, add paging or lazy-loading to improve performance.

Recommended Answers

All 7 Replies

Check out this link.

Then you can create 5 images in a table, or however you choose to do it, and set each to have a url of this thumbnailpage with a query string of file, and pass 'file' into it, with the file path.

html table to hold the images

How do I create html table using C#?Can you show me an example?

Thanks
Ani

You can use either the repeater control or something along those lines, or use the System.Web.UI.HtmlControls namespace with class Table();

Use HtmlTable or Table class??

Ani

Why use all those? Using server controls is costly, so a repeater would be best. Just create a repeater and bind all your images to an array, then bind it to the datasource. Basically, use the System.IO namespace, loop through each file in the directory and detect if it's a .jpg or .jpeg file, then add the path/filename to the array. After this completes, set the array to the datasource of the repeater and databind. It will automatically do the rest for you.

Thanks for your suggestion..I m really new to this ASP.Net world..Can you help me with some chunks of C# code...By datasource, do you mean a SQL database or something alike?How do I bind images to an array?Create an Image array?

Thanks
Ani

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.