Click here to Skip to main content
16,004,578 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am attempting to create an ImageList in VB.net, I have been looking for an example to guide me. All the samples I found use this similar phrase ImageList1.Images.Add(Image.FromFile(singleFile)). However, I encounter the error BC30456: 'Images' is not a member of 'Add.(Image.FromFile). Could I be missing something, or has there been a change in VB 2022?

VB
Private Sub btLoadImages_Click(sender As Object, e As EventArgs) Handles btLoadImages.Click
     ImageList1.Images.Clear()
     ListView1.Clear()
     oFD1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)
     oFD1.Title = "Open an Image File"
     oFD1.Filter = "JPEGS|*.jpg|GIFS|*.gif"
     Dim ofdResults As Integer = oFD1.ShowDialog()
     If ofdResults = DialogResult.Cancel Then
         Exit Sub
     End If
     Try
         Dim numOfFiles As Integer = oFD1.FileNames.Length
         Dim aryFilePaths(numOfFiles - 1) As String
         Dim counter As Integer = 0
         For Each singleFile In oFD1.FileNames
             aryFilePaths(counter) = singleFile
             ImageList1.Images.Add(Image.FromFile(singleFile))
             counter += 1
         Next
         ListView1.LargeImageList = ImageList1
         For i As Integer = 0 To counter - 1
             ListView1.Items.Add(aryFilePaths(i), i)
         Next i
     Catch err As Exception
         MessageBox.Show(err.Message)
     End Try
 End Sub

This is from a tutorial I am using here as a guide to learn what I need: https://www.homeandlearn.co.uk/extras/picture-viewer/pic-viewer-intro.html

What I have tried:

I have wandered the net looking for an example that worked but they all have the same line in it.
Posted
Updated 27-May-24 16:39pm
v5

1 solution

OK, your error message as shown doesn't match your code:
'Images' is not a member of 'Add.(Image.FromFile)
ImageList1.Images.Add(Image.FromFile(singleFile))
The code sample does not contain a '.' character after the call to Add - so either you hand typed the error or you hand typed the code into your question.

And If I copy your code and paste it into a VB WinForms app by adding four items - the button, ListView, OpenFileDialog, and ImageList - it compiles fine. And it runs without error if I press the button - the image is added to the list.

So it's not the code that is the problem if it is exactly as you show - it's either something do with the project type, the code isn't as you show, or most likely the error message doesn't relate to that code but to different code in the same app.

So start here How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] and look at the actual message very carefully - it contains a lot of info that you haven't given us!

Quote:
The text of the error does not seem to apply to this problem. I don't see any typing errors. I don't know what else to tell you. I don't know how you could run it, but I cannot.


Simple: now you give us the actual error message it's obvious.
Quote:
BC30456 'FromFile' is not a member of 'MediaTypeNames.Image'
A simple google for "MediaTypeNames" will show you it's part of the System.Net.Mime namespace, which has to do with eMail, not displays.
Since an ImageList requires a System.Drawing.Image and the two classes are not equivelant or related you get the error.
So it's likely that you have a using statement that references MediaTypeNames when what you need for this code is System.Windows.Forms, or a fully qualified name for the Image class.

See how much info you can get from an error message?
 
Share this answer
 
v2
Comments
Member 14711713 29-May-24 1:48am    
ImageList1.Images.Add(Image.FromFile(singleFile)) is the code I have used. The other was a mistake typed in. It still shows an error at Image.FromFile. I get the error message BC30456 'FromFile' is not a member of 'MediaTypeNames.Image' Looking at the excepted values I see graphic image types like Jpeg, Png, ... Click the error number I see, 'The member you have provided is not a member of the class. Error ID: BC30456' The text of the error does not seem to apply to this problem.
I don't see any typing errors. I don't know what else to tell you. I don't know how you could run it, but I cannot.

Edit: I got it to work. I could only guess it had to be a setting for VB.net. I changed the program to .Net 8 from .Net 6 and it ran. Don't know if that was the problem but it works.
OriginalGriff 29-May-24 2:37am    
Answer updated

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