How to display images from a folder along with image names in C#??
Plzz help

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string sBasePath = System.Web.HttpContext.Current.Request.ServerVariables["APPL_PHYSICAL_PATH"];
        if ( sBasePath.EndsWith("\\"))
            sBasePath = sBasePath.Substring(0,sBasePath.Length-1);

        sBasePath = sBasePath + "\\" + "pics";

        System.Collections.Generic.List<string> oList = new System.Collections.Generic.List<string>();

        string[] extensions = { "*.jpg", "*.png" };

        List<string> files = new List<string>(); 

        foreach (string filter in extensions) 
        {
            files.AddRange(System.IO.Directory.GetFiles(sBasePath, filter)); 
            oList.Add(System.IO.Path.GetFileName(filter));
        }


        repImages.DataSource = oList;
        repImages.DataBind();

Edited 3 Years Ago by Nick Evan: Fixed formatting

i want the images from d folder and the image name should be displayed in a C# form

Try using FileOpenDialog method to choose the image, and then PictureBox control to show the image inside of it:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog open = new OpenFileDialog();
                open.InitialDirectory = @"C:\";
                open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
                if (open.ShowDialog() == DialogResult.OK)
                {
                    pictureBox1.Image = new Bitmap(open.FileName);
                    pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
                }
            }
            catch (Exception)
            {
                throw new ApplicationException("Failed loading image");
            }
        }
    }

Hope it helps,

Mitja

This article has been dead for over six months. Start a new discussion instead.