Click here to Skip to main content
15,893,904 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I have taken input of 5 images from user and showed it in photogallery now I want these images in list view too. How should I pass them in list view simultaneously. I tried the below code but it shows images in photogallery not in listview.

VB
Private Sub BrowseMultipleFilesButton_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles BrowseMultipleFilesButton.Click
    Dim OpenFileDialog1 As New OpenFileDialog
    OpenFileDialog1.Filter =
    "Images (*.BMP;*.JPG;*.GIF,*.PNG,*.TIFF)|*.BMP;*.JPG;*.GIF;*.PNG;*.TIFF|" +
    "All files (*.*)|*.*"
    OpenFileDialog1.Multiselect = True
    Dim index As New Integer
    OpenFileDialog1.Title = "Select Photos"
    If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
    If OpenFileDialog1.FileNames.Length > 5 Then
    MessageBox.Show("Please select no more than 5 files")
    Exit Sub
    End If
    If OpenFileDialog1.FileNames.Length < 5 Then
    MessageBox.Show("Please select 5 files")
    Exit Sub
    End If
    For Each file As String In OpenFileDialog1.FileNames
    Dim imageControl As New PictureBox()
    imageControl.Height = 100
    imageControl.Width = 100
    Dim myCallback As New Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)
    Dim myBitmap As New Bitmap(file)
    Dim myThumbnail As Image = myBitmap.GetThumbnailImage(96, 96, myCallback, IntPtr.Zero)
    imageControl.Image = myThumbnail
    PhotoGallary.Controls.Add(imageControl)
    Dim imageListLarge As New ImageList()
    imageListLarge.Images.Add(myBitmap)
    ListView_images.LargeImageList = imageListLarge
    Me.Controls.Add(ListView_images)
    Next
    btn_Save.Enabled = True
    BrowseMultipleFilesButton.Enabled = False
    End If
    End Sub
Posted

1 solution

Of course you can do it by updating the appropriate image list and ListView content in sync (what's the problem?) but why? The problem is not that you cannot do it, the problem is that the image showing mechanism is designed for a very different functionality: showing a set of fixed images choosing them by their indices in a list of images. In this approach, the images themselves are not considered as part of data.

With a photo gallery, the image itself is the key part of data. So, instead of building a cumbersome work around, better change the design. If you want to stay with ListView (but why? are you sure?), one appropriate technique would be custom draw:
https://msdn.microsoft.com/en-us/library/system.windows.forms.listview.ownerdraw%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.listview.drawitem%28v=vs.110%29.aspx[^].

But who told you it should be the ListView (itself quite a cumbersome control)?
It would be way more natural to use System.Windows.Forms.DataGridView with some instances of System.Windows.Forms.DataGridViewImageCell, preferably by having one or more whole columns of the type System.Windows.Forms.DataGridViewImageColumn:
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewimagecolumn(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewimagecell%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview%28v=vs.110%29.aspx[^].

Much simpler, isn't it?

—SA
 
Share this answer
 
v2
Comments
Member 11657542 12-May-15 14:10pm    
your given solution is looking good but I can't understand it. I explain u actually what I want to do. I want to take exact 5 images input from user show it to user and save them in SQL-Database. If u have an another solution than my code please tell me. Thanks for your Kind Suggestion.
Sergey Alexandrovich Kryukov 12-May-15 15:20pm    
All right, I considered 3 approaches in the solution above, in 3 first paragraphs. You just confirmed that your images are actually part of real data (not UI style or something), because data comes from the user (or database, does not matter). I would consider only the 3rd variant, DataGridView. Did not understand the idea? There is nothing much to understand; you just need to take some DataGridView instance and start playing with it: defined columns, add DataGridViewImageColumn, and the you should use Image for the Data property of the cells in this columns.
—SA
Member 11657542 17-May-15 8:27am    
Thanks! your suggestion helped me.
Sergey Alexandrovich Kryukov 17-May-15 10:04am    
You are welcome.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900