How do I load image into the picture box in VB2005

 

 I have a problem on loading image into the visual basic 2005 IDE picture box.

If Visual Basic 6 code goes like this,

   Me.PictureBox1.Picture = LoadPicture(sImagePath)

 

how  do i load  it in  Visual Basic 2005



Answer this question

How do I load image into the picture box in VB2005

  • Abbott

    It's never too late to post a reply. Your solution was exactly what I needed. Many thanks.
  • kuponutcom

    Hi,

    The code works fine. But my problem is that if I want to delete the .jpg file while the program is runing, an exception is thrown: "The process cannot access the file 'a.jpg' because it is being used by another process."

    My question is: Is there a way to load a picture into a picture box, and allow the picture file to be deleted from disk

    Thanks.


  • mannis

    Hi Cyber,

    This is one way to do it in vb2005

    Dim FilePath As String = "C:\TempPicture.jpg"

    Try

    PictureBox1.Image = Image.FromFile(FilePath)

    Catch ex As Exception

    MessageBox.Show(ex.Message)

    End Try

    I hope this helps

    Abdullah Al-Rasheed


  • Bob Cohen

    No, there isn't a way to remove an open file from the computer while it's open, but you could instead copy it into the TEMP directory, delete the original, then load the version in the TEMP directory.

  • Notis

    Yes there IS a way to load a picture into a picture box, and allow the picture file to be deleted. . Here is some code: You need to use the FileStream Class...

    (Type this code in your General Declerations:)

    Imports System

    Imports System.IO

    Imports System.Text

    (Use this code to read the jpg:)

    Dim FileDir

    FileDir = "C:\File.jpg" 'This is the path of your jpg"

    Dim fs As New FileStream(FileDir, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)  'you can read the file IF there isnt another app that has exclusive Read/Write.

    Picture.Image = Image.FromStream(fs)  'put the image from fs into the picture box

    fs.Close() 'close the FileStream; Very important

    (When you want to delete the jpg file from your computer use this code:)

    My.Computer.FileSystem.DeleteFile(FileDir, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin, FileIO.UICancelOption.DoNothing)

    Let me know if this helps, although it may be a little late (ie: date of thread).



  • How do I load image into the picture box in VB2005