I have a folder which contains all images.I want the names of the images to be displayed in a combobox and when i click on a image the image should be displayed in a picturebox(in C#.net).plzz help me in the code

Dani AI

Generated

This is a compact, practical WinForms pattern that builds on 's file-listing idea and 's pointer about System.IO. It populates a ComboBox from a folder, shows the chosen image in a PictureBox, and avoids two common problems: locking the image file and leaking GDI resources. The sample uses data-binding for a clean ComboBox UI, filters common image extensions, clones the image into memory, and disposes the previous PictureBox image.

using System;
using System.IO;
using System.Linq;
using System.Drawing;
using System.Windows.Forms;

public partial class Form1 : Form
{
    private void Form1_Load(object sender, EventArgs e)
    {
        string folder = Path.Combine(Application.StartupPath, "Pictures"); // adjust as needed
        var allowed = new[] { ".jpg", ".jpeg", ".png", ".bmp", ".gif" };

        var items = Directory.Exists(folder)
            ? Directory.GetFiles(folder)
                .Where(f => allowed.Contains(Path.GetExtension(f).ToLowerInvariant()))
                .Select(f => new { Name = Path.GetFileName(f), FullPath = f })
                .ToList()
            : new System.Collections.Generic.List<object>();

        comboBox1.DisplayMember = "Name";
        comboBox1.ValueMember = "FullPath";
        comboBox1.DataSource = items;
        comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
        comboBox1.SelectedIndex = -1;

        pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        var path = comboBox1.SelectedValue as string;
        if (string.IsNullOrEmpty(path) || !File.Exists(path)) return;

        var previous = pictureBox1.Image;
        try
        {
            using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
            using (var img = Image.FromStream(fs))
            {
                pictureBox1.Image = new Bitmap(img); // clone so file is not locked
            }
        }
        catch
        {
            // log or handle as appropriate for the app
        }
        finally
        {
            previous?.Dispose();
        }
    }
}

Troubleshooting notes: the compile error about DirectoryInfo/FileInfo in the original posts is caused by a missing reference to System.IO (adding the proper using or using fully qualified names resolves it). Avoid Image.FromFile or new Bitmap(filename) because they keep the file locked until the Image is disposed; the FileStream + Image.FromStream + new Bitmap(img) pattern shown above creates a copy and releases the file. For large image sets, load filenames on the UI thread but load image data on a background thread to keep the UI responsive. This approach addresses the original errors, prevents file locks, and reduces memory/GDI leaks.

Recommended Answers

All 5 Replies

String[] paths = {"D:\\Project\\Flower-02-KayEss-1.jpg", "D:\\Project\\My Pictures\\123.png" };
        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Add("Flower");
            comboBox1.Items.Add("Table");
            comboBox1.Text = "Select";
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            pictureBox1.Image = new Bitmap(paths[comboBox1.SelectedIndex]);
        }

More detailed version

String[] paths; 
        private void Form1_Load(object sender, EventArgs e)
        {
            DirectoryInfo di = new DirectoryInfo(@"D:\Project\Pictures\");
            paths = new String[di.GetFiles().Count()];
            MessageBox.Show(paths.Length + "");
            int i = 0;
            foreach (FileInfo fi in di.GetFiles())
            {
                comboBox1.Items.Add(fi.Name);
            }
            foreach (FileInfo fi in di.GetFiles())
            {
                paths[i] = fi.FullName;
                i++;
            }

            comboBox1.Text = "Select";
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
           pictureBox1.Image = new Bitmap(paths[comboBox1.SelectedIndex]);

        }

I tried this code but i am getting errors
I am new to C#.plzz help me.The errors are:

Error 1 The type or namespace name 'DirectoryInfo' could not be found (are you missing a using directive or an assembly reference?)
Error 3 The type or namespace name 'FileInfo' could not be found (are you missing a using directive or an assembly reference?)

More detailed version

String[] paths; 
        private void Form1_Load(object sender, EventArgs e)
        {
            DirectoryInfo di = new DirectoryInfo(@"D:\Project\Pictures\");
            paths = new String[di.GetFiles().Count()];
            MessageBox.Show(paths.Length + "");
            int i = 0;
            foreach (FileInfo fi in di.GetFiles())
            {
                comboBox1.Items.Add(fi.Name);
            }
            foreach (FileInfo fi in di.GetFiles())
            {
                paths[i] = fi.FullName;
                i++;
            }

            comboBox1.Text = "Select";
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
           pictureBox1.Image = new Bitmap(paths[comboBox1.SelectedIndex]);

        }

I tried this code but i am getting errors
I am new to C#.plzz help me.The errors are:

Error 1 The type or namespace name 'DirectoryInfo' could not be found (are you missing a using directive or an assembly reference?)
Error 3 The type or namespace name 'FileInfo' could not be found (are you missing a using directive or an assembly reference?)

You need an extra using statement:

using System.IO;
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.