Displaying image on a Panel

LouisL picture LouisL · Dec 12, 2010 · Viewed 43.5k times · Source

this i a snippet of my code....

 public void btn_browse_Click_Click(object sender, EventArgs e)
    {

        try
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
            if (open.ShowDialog() == DialogResult.OK)
            {
                scan.Enabled = true;
                pic = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
                pic2 = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);                                   

                pic = new Bitmap(open.FileName);
                pic2 = new Bitmap(open.FileName);

                pictureBox1.Image = pic;
                pictureBox2.Image = pic2;
                pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
                textBox1.Text = open.FileName;
                pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;



            } // end of if opendialog


        } // end of try

        catch (Exception)
        {
            throw new ApplicationException("Failed loading image");
        }


    }

The question is: Am i able to display my image browsed on a Panel instead of a PictureBox ?

Answer

GunnarJ picture GunnarJ · Dec 12, 2010

You could set the BackGroundImage of the panel to the image.
panel.BackgroundImage = Image.FromFile(open.FileName);
Should do the trick.