Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

fill listbox with images

Posted on 2004-10-19
5
Medium Priority
?
330 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 500 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 500 total points
ID: 12354838
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

971 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