How to Add an Image in a ListView Programmatically in C#.

Submitted by: 
Language: 
Visitors have accessed this post 157 times.

If you want to learn how to add an image inside a ListView programmatically? This tutorial is just right for you. You may find it hard to do, but I will teach you the simplest way on how to do it. In this method, you will be able to add any image that you like and put it inside the ListView. Aside from that, you can also resize the image according to your desire.

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application for c#.
ps1

Step 2

Add a ListView inside the Form just like shown below.
ps2

Step 3

Press F7 to open the code editor. In the code editor, declare an item in the ListView control and an image list to control an image object.

  1.  
  2. ListViewItem lstviewItem;
  3. ImageList lstviewItemImageList = new ImageList();

Step 4

Create a method for adding an image in the ListView.

  1.  
  2. private void lst_items()
  3. {
  4. try
  5. {
  6.  
  7. string[] file;
  8. file = new string[] { "C:\\report.png", "C:\\maintenance.png", "C:\\computer logo.jpg", "C:\\damage.png" };
  9. foreach (string files in file)
  10. {
  11.  
  12. lstviewItem = new ListViewItem(files);
  13. lstviewItemImageList.ImageSize = new Size(50, 50);
  14. listView1.SmallImageList = lstviewItemImageList;
  15. lstviewItem.ImageIndex = lstviewItemImageList.Images.Add(Image.FromFile(lstviewItem.Text), Color.Transparent);
  16. listView1.Items.Add(lstviewItem);
  17. }
  18.  
  19. }
  20. catch (Exception ex)
  21. {
  22. MessageBox.Show("error : " + ex.Message);
  23. }
  24. }

Step 5

Add the following code for setting up the properties and adding the items in a ListView in the first load of the form.

  1.  
  2. private void Form1_Load(object sender, EventArgs e)
  3. {
  4. listView1.View = View.Details;
  5. listView1.Columns.Add("File");
  6. listView1.Columns[0].Width = 500;
  7.  
  8. lst_items();
  9.  
  10. }

The complete source code is included. You can download it and run it on your computer.
For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below.


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.