picturebox and openfiledialog

This is a discussion on picturebox and openfiledialog within the C# Programming forums, part of the General Programming Boards category; How can i take a file from an openfiledialog and show it in a picturebox. I have tried i like ...

  1. #1
    Registered User BraneMxm's Avatar
    Join Date
    Apr 2007
    Location
    Croatia
    Posts
    20

    picturebox and openfiledialog

    How can i take a file from an openfiledialog and show it in a picturebox.
    I have tried i like this but it wont work.

    Thanks in advance

    Code:
    private void button1_Click(object sender, EventArgs e)
            {
                openFileDialog1.ShowDialog();
                pictureBox1.BackgroundImage = Image.FromFile(openFileDialog1.FileName);
                pictureBox1.Refresh();
    
            }

  2. #2
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120
    I encapsulated mine to check the button that returned but otherwise i tried your code and it works fine for me. Although, I do use Image rather than BackgroundImage



    Code:
    DialogResult dResult = openFileDialog1.ShowDialog();
                if (dResult == DialogResult.OK)
                {
                    pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
                    pictureBox1.Refresh();
                }
    try an image instead of image background just to see.

    Does it show anything? I also set my

    Code:
    pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
    what sort of images are you trying? I used a jpeg and gif.
    Also you could do like a

    Code:
    MessageBox.Show(openFileDialog1.FileName);
    before it sets it just to see if its the proper file.
    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21