Solved

fill listbox with images

Posted on 2004-10-19
5
326 Views
Last Modified: 2010-08-05
hi
how can i fill a listbox with image items instead of text in c#? i've seen listboxes filles with images before.
huda
0
Comment
Question by:huda_qadura
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
aacool earned 125 total points
ID: 12352336
You need to associated an image list with the listbox and fill the image list with the images to display. Then set the List view with the view: listView1.View = View.SmallIcon;

Here is an example for a listbox that gets the files in a directory and shows the images as thumbnails along with the file name.

void MenuItem2Click(object sender, System.EventArgs e)
{
      try
      {

            FolderBrowserDialog fbDlg = new FolderBrowserDialog();
            fbDlg.Description = "Select Image Directory";
            DialogResult dlgRst = fbDlg.ShowDialog();
            if(dlgRst == DialogResult.OK)
            {
                  DirectoryInfo di = new DirectoryInfo(fbDlg.SelectedPath);
                  FileInfo[] files = di.GetFiles();
                  int indx=0;
                  Cursor.Current = Cursors.WaitCursor;
                  listView1.BeginUpdate();
                  foreach(FileInfo fi in files)
                  {
                        if(isValidExt(fi.Extension))
                        {      
                                                                // helper is a helper image library
                                                                // easy and complex impl. possible for this
                                                                // or use Image.LoadFromFile()
                        Image img = helper.openImagefi.FullName);
                        helper.resizeImage(img,ref img,new Size(60,60));
                        imgLst.Images.Add(img);
                        ListViewItem lvItem = new ListViewItem(fi.FullName,indx);
                        listView1.Items.Add(lvItem);
                        indx++;
                  }
            }
            listView1.SmallImageList = imgLst;
            listView1.View = View.SmallIcon;
            listView1.EndUpdate();
            Cursor.Current = Cursors.Default;
            this.Update();
            }
      }                        
      catch(Exception ex)
      {
            MessageBox.Show(ex.Message+ex.StackTrace);
      }
}

private bool isValidExt(string ext)
{
      bool valid = false;
      if(ext==".jpg" || ext=="*.bmp" || ext=="*.gif")
            valid = true;
      return valid;
}
0
 
LVL 3

Expert Comment

by:aacool
ID: 12352360
Easy impl of helper.openImage and helper.resizeImage
            // Open Image and return it - does not change size,etc.
            public Image openImage(string path)
            {
                  try
                  {
                        Bitmap bmp = new Bitmap(path);
                        m_img = (Image)bmp;
                        return m_img;
                  }
                  catch(Exception ex)
                  {
                        MessageBox.Show(ex.Message+"Path:"+path);
                        return (Image)null;
                  }
                  
            }
            public void resizeImage(Image src_image,ref Image newImg,Size size)
            {
                Bitmap bitmap = new Bitmap(size.Width, size.Height,src_image.PixelFormat);
                  Graphics new_g = Graphics.FromImage(bitmap);
                  new_g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                  new_g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                  new_g.DrawImage(src_image, 0, 0, bitmap.Width, bitmap.Height);
                  newImg = (Image)bitmap;
                  new_g.Dispose();                  
            }
0
 
LVL 18

Assisted Solution

by:armoghan
armoghan earned 125 total points
ID: 12354838
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question