LouisL
LouisL

Reputation: 121

Displaying image on a Panel

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 ?

Upvotes: 8

Views: 50157

Answers (4)

Javed Akram
Javed Akram

Reputation: 15354

Basically PictureBox is made to display images while Panel is for drawing in it(curves, lines, rectangles,....)

So I suggest better to use pictureBox, but if you want to show image in panel.

  1. You can draw it on paint event
    OR
  2. Use BackgroundImage property.

Upvotes: 7

Jeff Ogata
Jeff Ogata

Reputation: 57803

For winforms, you can use the BackgroundImage and BackgroundImageLayout properties of the Panel.

Upvotes: 1

GunnarJ
GunnarJ

Reputation: 440

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

Upvotes: 12

Abe Miessler
Abe Miessler

Reputation: 85096

Yes. The Panel class has a member called BackImageUrl. Just specify the URL of the picture you want to use as the background.

Upvotes: 1

Related Questions