6 Replies - 839 Views - Last Post: 31 January 2013 - 11:34 PM Rate Topic: -----

#1 jnova  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-January 13

How to display a picture in a picturebox from a file on PC

Posted 21 January 2013 - 09:57 PM

I'm trying to make a Picture viewer in Visual Basic 2012 and I can't get it display in the picturebox

Public Class Form1

    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        OpenFileDialog1.Title = "Please Select a File"
        OpenFileDialog1.InitialDirectory = "C:temp"
        OpenFileDialog1.ShowDialog(PictureBox1)

        Button1.Visible = True
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Button2.Visible = True
        Application.Exit()
    End Sub

    Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        PictureBox1.Text = OpenFileDialog1.FileName.ToString()
    End Sub

    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click

    End Sub


End Class


Is This A Good Question/Topic? 0
  • +

Replies To: How to display a picture in a picturebox from a file on PC

#2 kai_itz me  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 159
  • Joined: 03-August 12

Re: How to display a picture in a picturebox from a file on PC

Posted 21 January 2013 - 10:02 PM

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.PictureBox1.BackgroundImage = Image.FromFile("C:\Documents and Settings\kai\My Documents\My Pictures\d1.jpg")
    End Sub


D1 is the name of picture to be displayed

This post has been edited by kai_itz me: 21 January 2013 - 10:03 PM

Was This Post Helpful? 0
  • +
  • -

#3 jnova  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-January 13

Re: How to display a picture in a picturebox from a file on PC

Posted 21 January 2013 - 10:10 PM

View Postkai_itz me, on 21 January 2013 - 10:02 PM, said:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.PictureBox1.BackgroundImage = Image.FromFile("C:\Documents and Settings\kai\My Documents\My Pictures\d1.jpg")
    End Sub


D1 is the name of picture to be displayed


I'm trying to use the OpenFileDialog command, so the user selects a picture from their Computer and the program displays it
Was This Post Helpful? 0
  • +
  • -

#4 kai_itz me  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 159
  • Joined: 03-August 12

Re: How to display a picture in a picturebox from a file on PC

Posted 21 January 2013 - 10:29 PM

in the picturebox1 properties u can set backgroundimagelayout to strech.
this will show full image

Public Class Form1


    Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

        OpenFileDialog1.Title = "Please Select a File"

        OpenFileDialog1.InitialDirectory = "C:temp"

        OpenFileDialog1.ShowDialog(PictureBox1)

        Me.PictureBox1.BackgroundImage = Image.FromFile("C:\Documents and Settings\kai\My Documents\My Pictures\d1.jpg")
        PictureBox1.Visible = True
        Button1.Visible = True

    End Sub



    Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click

        Me.Close()

    End Sub



    Private Sub OpenFileDialog1_FileOk(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk

        PictureBox1.Text = OpenFileDialog1.FileName.ToString()

    End Sub





End Class



Was This Post Helpful? 0
  • +
  • -

#5 jnova  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-January 13

Re: How to display a picture in a picturebox from a file on PC

Posted 21 January 2013 - 11:00 PM

View Postkai_itz me, on 21 January 2013 - 10:29 PM, said:

in the picturebox1 properties u can set backgroundimagelayout to strech.
this will show full image

Public Class Form1


    Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

        OpenFileDialog1.Title = "Please Select a File"

        OpenFileDialog1.InitialDirectory = "C:temp"

        OpenFileDialog1.ShowDialog(PictureBox1)

        Me.PictureBox1.BackgroundImage = Image.FromFile("C:\Documents and Settings\kai\My Documents\My Pictures\d1.jpg")
        PictureBox1.Visible = True
        Button1.Visible = True

    End Sub



    Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click

        Me.Close()

    End Sub



    Private Sub OpenFileDialog1_FileOk(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk

        PictureBox1.Text = OpenFileDialog1.FileName.ToString()

    End Sub





End Class




Thanks that worked
Was This Post Helpful? 0
  • +
  • -

#6 kai_itz me  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 159
  • Joined: 03-August 12

Re: How to display a picture in a picturebox from a file on PC

Posted 21 January 2013 - 11:30 PM

the above will show only a particular file...(sorry i missread)
i have corrected this
the below code will show the selected file from a particular location.

 Dim img As String

    Public Sub OpenImage()

        'Set the Filter.  

        OpenFileDialog1.Filter = "Bitmap |*.bmp| JPG | *.jpg | GIF | *.gif | All Files|*.*"



        'Clear the file name  

        OpenFileDialog1.FileName = ""



        'Show it  

        If OpenFileDialog1.ShowDialog(Me) = DialogResult.OK Then

            'Get the image name  



            img = OpenFileDialog1.FileName

            'Create a new Bitmap and display it  

            PictureBox1.Image = System.Drawing.Bitmap.FromFile(img)

        End If

    End Sub


    Private Sub Form6_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        PictureBox1.Visible = True
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        OpenImage()
    End Sub


This post has been edited by kai_itz me: 21 January 2013 - 11:37 PM

Was This Post Helpful? 0
  • +
  • -

#7 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: How to display a picture in a picturebox from a file on PC

Posted 31 January 2013 - 11:34 PM

jnova, please read http://www.dreaminco...his-is-old-vb6/ .
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1