none
How to assigning a picture to a picture box at run time in VB.NET 2010 RRS feed

  • Question

  • swaby asked on <button class="msaActionText msgHistoryBtn" jQuery17105737122842210125="41" title="See post history" type="button">September 5, 2012</button>

    having problem assigning a picture to a picture box at run time

    I am having problem assigning a picture to the picturebox in VB 2012, In earlier VB I would load the image to the image box from a file example imgname.Picture = 'C:\application\pictutes\picname.jpg" The image is suppose to be loaded depending on what the users selection is. There can be any  1 of 50 pictures load. Now I cannot even think how it should be done. I tried everything I know. No luck. Any help?.

     

    Actually here are the statements I used in older version

    I would declair the 'LocalDir" then

    LocalDir = "C:\Application\Microsoft Application\VB6\Project"

    then i would load 2 image boxes depending on the selections as:

    imgStateMap.Picture = LoadPicture(LocalDir  & "\states\Albama02.Wmf")
     imgStateFlag.Picture = LoadPicture(LocalDir & "\Flags\alflag.gif")

     

    Thursday, September 6, 2012 3:43 PM

Answers

  • Something likce this;

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            PictureBox1.ImageLocation = ("C:\Users\John\Desktop\Crossbones.Bmp")
            PictureBox1.Load()
        End Sub


    You've taught me everything I know but not everything you know.

    • Marked as answer by swabie Thursday, September 6, 2012 7:44 PM
    Thursday, September 6, 2012 3:52 PM

All replies

  • Something likce this;

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            PictureBox1.ImageLocation = ("C:\Users\John\Desktop\Crossbones.Bmp")
            PictureBox1.Load()
        End Sub


    You've taught me everything I know but not everything you know.

    • Marked as answer by swabie Thursday, September 6, 2012 7:44 PM
    Thursday, September 6, 2012 3:52 PM
  • To avoid that it locks use

    YourPicturebox.Image = Image.FromFile(yourPath)


    Success
    Cor

    Thursday, September 6, 2012 3:57 PM
  • This may be of help http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.aspx#Y0

    You've taught me everything I know but not everything you know.

    Thursday, September 6, 2012 4:02 PM
  • Set the Image property using the FromFile method of the Image class.

    In the example below, the path set for the location of the image is the My Documents folder. This is done, because you can assume that most computers running the Windows operating system will include this directory. This also allows users with minimal system access levels to safely run the application. The example below assumes a form with a PictureBox control already added. 

    Private Sub LoadNewPict()
          PictureBox1.Image = Image.FromFile _
       (System.Environment.GetFolderPath _
       (System.Environment.SpecialFolder.Personal) _
       & "\Image.gif")
    End Sub
    
    Thursday, September 6, 2012 4:02 PM
  • You can also go into Tab Project and down at the bottom select (application name) properties. Select on the left side Resources, select the down arrow beside Add Resources, select Existing file and browse to select the Image file you want to add as a resource to your code.

    Then you can call it by the below code as an example.

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            PictureBox1.Image = My.Resources.Crossbones 'where Crossbones is the name of the resource file being used by me.
        End Sub


    You've taught me everything I know but not everything you know.


    Thursday, September 6, 2012 4:21 PM