Solved

fill listbox with images

Posted on 2004-10-19
5
320 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now