Solved

fill listbox with images

Posted on 2004-10-19
5
324 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
  • 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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 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