locked
How to load an image into picturebox RRS feed

  • Question

  • I am using VB.net and have a form that contains a picturebox and i have to load an image into it from another class. When i set image property of picturebox from other class it doesn't display the image. I think its something to do with display form intitialization but not sure. It works well if i put a button on the same form to display image but thats not what i want. I want to set the image from another class's function.

    Please Help

    Thanx 



    I have a form with public class VBSample and this form contains a button "Take Picture" with an associated event

    controller.actionPerformed(

    "takepicture")
    Where controller is a controller for my attached camera. From here it goes to a controller handler of

    Public

     

    Class CameraController
    which actually takes the picture from camera. After taking the picture program control is transeferd to

    Public

     

    Class DownloadCommand
    which download the image from camera into a filestream and then stores it on hard disk...after copying the image into memory i want to display it in a picture box on my initial form which is

    Public Class VBSample
    but it says that picturebox cannot be referenced ....  

    I hope this time its a more clear for everyone ....

    Thank you everybody for your initial responses...

    Thursday, November 26, 2009 10:37 AM

Answers

  • Assalam-o-Alykum Awais Hassan !

    In VB.NET Many Source Of Load Images In PictureBox ;

    Load Images via OpenFileDialog :
    Dim result As DialogResult = OpenFileDialog1.ShowDialog
            If result = Windows.Forms.DialogResult.OK Then
                If (OpenFileDialog1.FileName IsNot Nothing) Or (OpenFileDialog1.FileName <> String.Empty) Then
    
                    PictureBox1.BackgroundImage = Image.FromFile(OpenFileDialog1.FileName)
                    PictureBox1.BackgroundImageLayout = ImageLayout.Zoom
    
                End If
            End If
    

    O R

    Dim ofd As New OpenFileDialog
            If ofd.ShowDialog = DialogResult.OK Then
                If ofd.FileName <> String.Empty Then
                    Me.myPictureBox.Image = Bitmap.FromFile(ofd.FileName)
                End If
            End If


    Load Images via Clipboard :
    PictureBox1.SizeMode() = PictureBoxSizeMode.Zoom
    PictureBox1.Image() = Clipboard.GetImage()
    

    Load Images From Resource :
    PictureBox1.Image = My.Resources.<Name_of_res_file>


    www.shariqdon.media.officelive.com
    • Marked as answer by YiChun Chen Monday, November 30, 2009 10:27 AM
    Thursday, November 26, 2009 9:18 PM
  • I am using VB.net and have a form that contains a picturebox and i have to load an image into it from another class. When i set image property of picturebox from other class it doesn't display the image. I think its something to do with display form intitialization but not sure. It works well if i put a button on the same form to display image but thats not what i want. I want to set the image from another class's function.

    Please Help

    Thanx 

    Hi Awais Hassan,

    With One Button and One PictureBox on a FORM try this code please.

    It loads via another class that I have called ImageRoutines.    :-)


    Regards,

    John


    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim ImgRoutine1 As New ImageRoutines
            ImgRoutine1.LoadImage(PictureBox1)
    
        End Sub
    End Class
    
    Public Class ImageRoutines
    
        Public Sub LoadImage(ByVal aPictureBox As PictureBox)
    
            Dim ofd As New OpenFileDialog
            ofd.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyPictures
            ofd.Filter = "JPEG files (*.jpg)|*.jpg|Bitmap files (*.bmp)|*.bmp"
            Dim result As DialogResult = ofd.ShowDialog
            If Not (aPictureBox) Is Nothing And ofd.FileName <> String.Empty Then
                aPictureBox.Image = Image.FromFile(ofd.FileName)
            End If
    
        End Sub
    
        Public Sub LoadBackgroundImage(ByVal aPictureBox As PictureBox)
    
            Dim ofd As New OpenFileDialog
            ofd.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyPictures
            ofd.Filter = "JPEG files (*.jpg)|*.jpg|Bitmap files (*.bmp)|*.bmp"
            Dim result As DialogResult = ofd.ShowDialog
            If Not (aPictureBox) Is Nothing And ofd.FileName <> String.Empty Then
                aPictureBox.BackgroundImage = Image.FromFile(ofd.FileName)
                aPictureBox.BackgroundImageLayout = ImageLayout.Zoom
            End If
    
        End Sub
    
    End Class

    I'm currently looking for work in Vb.Net software development. :-)
    • Marked as answer by YiChun Chen Monday, November 30, 2009 10:27 AM
    Friday, November 27, 2009 3:44 AM
  • Because they give you all those difficult answers, I will give you the correct one based on your question.

    YourPictureBox.Image = TheImage
    Success
    Cor
    • Marked as answer by YiChun Chen Monday, November 30, 2009 10:27 AM
    Friday, November 27, 2009 7:28 AM

All replies

  • You mean a schoolclass?

    If you don't describe a little bit what the public members are from that class, then it is hard to answer you.

    You can of course also show the code from that class that is public.

     
    Success
    Cor
    Thursday, November 26, 2009 11:27 AM
  • That should work. You should probably dig around in the debugger and make sure it's really doing what you think it is.

    For example, how are you getting the PictureBox to the other class? Are you passing it as a parameter to a method or something?

    Rod
    www.vb-helper.com
    Thursday, November 26, 2009 2:15 PM
  • I am using VB.net and have a form that contains a picturebox and i have to load an image into it from another class. When i set image property of picturebox from other class it doesn't display the image. I think its something to do with display form intitialization but not sure. It works well if i put a button on the same form to display image but thats not what i want. I want to set the image from another class's function.

    Please Help

    Thanx 

    why you dont load your picture to your resource? that should easer you work.
    Don't judge me, just Upgrade me. Thanks!
    Thursday, November 26, 2009 6:41 PM
  • Assalam-o-Alykum Awais Hassan !

    In VB.NET Many Source Of Load Images In PictureBox ;

    Load Images via OpenFileDialog :
    Dim result As DialogResult = OpenFileDialog1.ShowDialog
            If result = Windows.Forms.DialogResult.OK Then
                If (OpenFileDialog1.FileName IsNot Nothing) Or (OpenFileDialog1.FileName <> String.Empty) Then
    
                    PictureBox1.BackgroundImage = Image.FromFile(OpenFileDialog1.FileName)
                    PictureBox1.BackgroundImageLayout = ImageLayout.Zoom
    
                End If
            End If
    

    O R

    Dim ofd As New OpenFileDialog
            If ofd.ShowDialog = DialogResult.OK Then
                If ofd.FileName <> String.Empty Then
                    Me.myPictureBox.Image = Bitmap.FromFile(ofd.FileName)
                End If
            End If


    Load Images via Clipboard :
    PictureBox1.SizeMode() = PictureBoxSizeMode.Zoom
    PictureBox1.Image() = Clipboard.GetImage()
    

    Load Images From Resource :
    PictureBox1.Image = My.Resources.<Name_of_res_file>


    www.shariqdon.media.officelive.com
    • Marked as answer by YiChun Chen Monday, November 30, 2009 10:27 AM
    Thursday, November 26, 2009 9:18 PM
  • I am using VB.net and have a form that contains a picturebox and i have to load an image into it from another class. When i set image property of picturebox from other class it doesn't display the image. I think its something to do with display form intitialization but not sure. It works well if i put a button on the same form to display image but thats not what i want. I want to set the image from another class's function.

    Please Help

    Thanx 

    Hi Awais Hassan,

    With One Button and One PictureBox on a FORM try this code please.

    It loads via another class that I have called ImageRoutines.    :-)


    Regards,

    John


    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim ImgRoutine1 As New ImageRoutines
            ImgRoutine1.LoadImage(PictureBox1)
    
        End Sub
    End Class
    
    Public Class ImageRoutines
    
        Public Sub LoadImage(ByVal aPictureBox As PictureBox)
    
            Dim ofd As New OpenFileDialog
            ofd.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyPictures
            ofd.Filter = "JPEG files (*.jpg)|*.jpg|Bitmap files (*.bmp)|*.bmp"
            Dim result As DialogResult = ofd.ShowDialog
            If Not (aPictureBox) Is Nothing And ofd.FileName <> String.Empty Then
                aPictureBox.Image = Image.FromFile(ofd.FileName)
            End If
    
        End Sub
    
        Public Sub LoadBackgroundImage(ByVal aPictureBox As PictureBox)
    
            Dim ofd As New OpenFileDialog
            ofd.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyPictures
            ofd.Filter = "JPEG files (*.jpg)|*.jpg|Bitmap files (*.bmp)|*.bmp"
            Dim result As DialogResult = ofd.ShowDialog
            If Not (aPictureBox) Is Nothing And ofd.FileName <> String.Empty Then
                aPictureBox.BackgroundImage = Image.FromFile(ofd.FileName)
                aPictureBox.BackgroundImageLayout = ImageLayout.Zoom
            End If
    
        End Sub
    
    End Class

    I'm currently looking for work in Vb.Net software development. :-)
    • Marked as answer by YiChun Chen Monday, November 30, 2009 10:27 AM
    Friday, November 27, 2009 3:44 AM
  • Because they give you all those difficult answers, I will give you the correct one based on your question.

    YourPictureBox.Image = TheImage
    Success
    Cor
    • Marked as answer by YiChun Chen Monday, November 30, 2009 10:27 AM
    Friday, November 27, 2009 7:28 AM
  • Cor is right. But you should publish your code, because it seems that is not very clear what you do.

    Best regards, Sergiu
    Friday, November 27, 2009 10:06 AM