none
How do I bring pics into a picturebox using code?

    Question

  • I have a folder of images located in the same folder where the SLN file is.
    How do I use code to display said pictures pictureboxs?

    Monday, April 10, 2017 6:37 PM

Answers

  • I said they were JPG and I now have one simple line of code that allows me to place the folder almost anywhere.  With a little bit of extra code, I can even have the user apply a flash drive with pics.

    I solved by own problem.

    • Marked as answer by GarySutcliff Monday, April 10, 2017 10:18 PM
    Monday, April 10, 2017 10:09 PM

All replies

  • There are plenty of examples of how to display images. Below is just a couple.

    https://code.msdn.microsoft.com/windowsdesktop/Picture-gallery-in-06150f45

    ImageViewer project:

    https://www.codeproject.com/Articles/26703/Image-Viewer-User-Control-with-Preview-Capabilit


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Monday, April 10, 2017 7:15 PM
  • Hi

    Here is some very basic  code to do what you ask.

    ' Form1 with PictureBox1
    ' and ComboBox1
    ' no error checking included
    Option Strict On
    Option Explicit On
    Option Infer Off
    Public Class Form1
        ' set path to Pictures
        Dim PicPath As String = "C:\Users\Les\Documents\JigSaw"
    
        Dim Pics As New Dictionary(Of String, String)
        Dim PicTypes As New List(Of String)
        Private Sub Form1_Load(sender As System.Object, e As EventArgs) Handles MyBase.Load
            ' set up picture types to allow
            PicTypes.AddRange({".jpeg", ".jpg", ".bmp"})
    
            ' keep aspect ratio
            PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
    
            ' OR just stretch to picturebox size
            ' PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
    
            Dim p As ObjectModel.ReadOnlyCollection(Of String) = My.Computer.FileSystem.GetFiles(PicPath, FileIO.SearchOption.SearchAllSubDirectories)
            For i As Integer = p.Count - 1 To 0 Step -1
                If PicTypes.Contains(IO.Path.GetExtension(p(i))) Then
                    Pics.Add(IO.Path.GetFileNameWithoutExtension(p(i)), p(i))
                End If
            Next
            ComboBox1.DataSource = New BindingSource(Pics, Nothing)
            ComboBox1.DisplayMember = "key"
            ComboBox1.ValueMember = "value"
            ComboBox1.SelectedIndex = 0
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
            Dim cb As ComboBox = DirectCast(sender, ComboBox)
            Dim kvp As KeyValuePair(Of String, String) = CType(cb.SelectedItem, KeyValuePair(Of String, String))
            PictureBox1.Image = Image.FromFile(kvp.Value)
        End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Monday, April 10, 2017 7:23 PM
    Monday, April 10, 2017 7:22 PM
  • TMI, TMI, TMI, TMI...

    Timers? "We don't need no stinkin" timers.... (:

    Is there not a simple one-line statement that will find a specific picture in a folder located where the SNL file is and place it into a picturebox?  If the folder needs to be else where, tell me where to stick it.

    I am guessing at this but: Picturebox1.ImageLocation = [path to the picture]\pic1.jpg

    If I need to arrange for settings, I will see to that later.  I just get a pic into the program for now.

    Monday, April 10, 2017 7:36 PM
  • After adding the folder and images to the solution, expand the folder in the solution explorer, select all of the files, then go to the Properties window and set the Copy to Output Directory property to Copy if newer.

    With that done, you can load the files at runtime using:

    PictureBox1.Image = Bitmap.FromFile("Directory Name\image name.img")


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Monday, April 10, 2017 7:40 PM
    Moderator
  • I have a folder of images located in the same folder where the SLN file is.
    How do I use code to display said pictures pictureboxs?


    If they're not excessively large {total file size}, then why not put them in the resources?

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Monday, April 10, 2017 7:47 PM
  • Cramming them into VB would make it difficult to make changes.  Why place them deep into the software?  Is there any benefit to that?

    So far no one has said that I cannot do what I want to do, yet no one has offered a simple solution.

    For now, as a test program, I want them to have easy access.


    Monday, April 10, 2017 8:13 PM
  • Cramming them into VB would make it difficult to make changes.  Why place them deep into the software?  Is there any benefit to that?

    So far no one has said that I cannot do what I want to do, yet no one has offered a simple solution.

    I presume that you're addressing me?

    If so, then the reason is that it's not file-based, but if you insist on simplicity first, then put them online and use the PictureBox's .ImageLocation property (or better yet, the .LoadAsync method).


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Monday, April 10, 2017 8:18 PM
  • Gary,

    It can't get simpler than "one line of code" as shown by Reed.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Monday, April 10, 2017 8:36 PM
  • Cramming them into VB would make it difficult to make changes.  Why place them deep into the software?  Is there any benefit to that?

    So far no one has said that I cannot do what I want to do, yet no one has offered a simple solution.

    For now, as a test program, I want them to have easy access.

    Hi

    Well, you have had an offering of a 'simple solution', or was my offering to difficult for you.


    Regards Les, Livingston, Scotland

    Monday, April 10, 2017 8:45 PM
  • I have yet to see a "simple" line of code.
    Not to worry, I give up, I am going to do something different.


    Monday, April 10, 2017 8:46 PM
  • ...or was my offering to difficult for you.

    Regards Les, Livingston, Scotland

    Apparently he thinks that if it's more than one line of code, it's grossly excessive.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Monday, April 10, 2017 8:47 PM
  • I have yet to see a "simple" line of code.
    Not to worry, I give up, I am going to do something different.


    Hi

    I am sorry that we were unable to give you a 'simple' line of code. You didn't even see fit to supply some basic information for us to work with - such as - what are the file/picture types you are dealing with.

    Hope you have more success in asking questions regarding the 'something different'.


    Regards Les, Livingston, Scotland

    Monday, April 10, 2017 8:51 PM
  • I said they were JPG and I now have one simple line of code that allows me to place the folder almost anywhere.  With a little bit of extra code, I can even have the user apply a flash drive with pics.

    I solved by own problem.

    • Marked as answer by GarySutcliff Monday, April 10, 2017 10:18 PM
    Monday, April 10, 2017 10:09 PM
  • I said they were JPG and I now have one simple line of code that allows me to place the folder almost anywhere.  With a little bit of extra code, I can even have the user apply a flash drive with pics.

    I solved by own problem.

    Where? Where within your OP is JPG mentioned? I have read this several times and failed to find it!

    OP:

    I have a folder of images located in the same folder where the SLN file is.
    How do I use code to display said pictures pictureboxs?


    Regards Les, Livingston, Scotland

    Monday, April 10, 2017 10:17 PM
  • I solved by own problem.

    You might like to share your solution with the forum members, so everyone understands how they might better respond to similar queries in the future. 

    If you want suitable answers to your question it is important to consider how you ask it.  Your initial post mentioned "I have a folder of images ..." so there was a presumption that you wanted to display more than one, but it wasn't clear whether that was several at one time in several different picture boxes, or several sequentially in one picture box.  Your question also didn't indicate whether or not you could assume what the name of the image was, or if it had to be discovered by looking at the files in the folder.  You didn't indicate that the files would be JPG (other than in a subsequent example) and, most importantly, you didn't specify that the code had to be in one line.

    If you had provided that detail I am confident that you would have got the response you needed much more promptly.  As it was, you got a number of responses from which to choose your solution.

    Monday, April 10, 2017 10:19 PM

  • Where? Where within your OP is JPG mentioned? I have read this several times and failed to find it!

    OP:

    I have a folder of images located in the same folder where the SLN file is.
    How do I use code to display said pictures pictureboxs?


    Regards Les, Livingston, Scotland

    Les,

    Were you around when they had medals instead of the current system?

    There was a guy here who would post a question - he'd get lots of replies and no matter how logically correct they were, he'd only say something like "it didn't work", never explaining it more.

    He'd then pop in and say that'd he'd solved it himself but when challenged to show the code that he used to do that -- he went silent.

    Back then he'd get the points for having answered the question; they changed the system so that no longer happens.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Monday, April 10, 2017 10:25 PM
  • @Frank

    Yes, the medals were in use when I joined.

    Can be very frustrating at times :)

    '

    I see the OP has done just that - clearly he has no concept of what constitutes an answer :)


    Regards Les, Livingston, Scotland


    • Edited by leshay Monday, April 10, 2017 10:54 PM
    Monday, April 10, 2017 10:51 PM
  • I have yet to see a "simple" line of code.
    Not to worry, I give up, I am going to do something different.



    Seriously you need to be a little more specific in your questions. We are not mind readers so there is a reason why you didn't get a simple answer to a somewhat vague question.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Monday, April 10, 2017 11:06 PM