Solved

fill listbox with images

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
which object should i use (efficiency) 5 45
what technologies offer Authentication over Web Services? 4 104
Export import database 4 43
Server Error 11 47
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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 Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

910 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