All posts tagged images

Show all images in a directory asp.net

How can we show all image from a particular directory in asp.net.

  <asp:DataList ID="dlQLImages" runat="server">  
      <ItemTemplate>  
        <img src='<%# string.Format("~/Images/{0}", DataBinder.Eval(Container.DataItem,"Name")) %>'  
            height="250" width="400" runat="server" />  
      </ItemTemplate>  
  </asp:DataList>  
   If Not IsPostBack Then  
        Dim objDI As New System.IO.DirectoryInfo(Server.MapPath("~/Images"))  
        Me.dlQLImages.DataSource = objDI.GetFiles("*.png")  
       Me.dlQLImages.DataBind()  
    End If  

or you can use

 
  DirectoryInfo diImages = new DirectoryInfo(Server.MapPath("~/images/Slider/"));

            
            ArrayList alImages = new ArrayList();
            // GetFiles method doesn't allow us to filter for multiple
            // file extensions, so we must find images in four steps
            // Feel free to add new or remove existing extension to
            // suit your needs
            alImages.AddRange(diImages.GetFiles("*.gif"));
            alImages.AddRange(diImages.GetFiles("*.jpg"));
            alImages.AddRange(diImages.GetFiles("*.bmp"));
            alImages.AddRange(diImages.GetFiles("*.png"));

            dtlSlider.DataSource =alImages;
            dtlSlider.DataBind();