在面板上显示图像

时间:2010-12-12 07:15:45

标签: c# .net panel image

这是我的代码片段....

 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");
        }


    }

问题是:我能否在Panel而不是PictureBox上显示我的图像?

4 个答案:

答案 0 :(得分:11)

您可以将面板的BackGroundImage设置为图像 panel.BackgroundImage = Image.FromFile(open.FileName);
应该做的伎俩。

答案 1 :(得分:4)

PictureBox 基本上显示图片,而 Panel 用于绘制 >在其中(曲线,线条,矩形,......)

所以我建议最好使用pictureBox,但如果你想在面板中显示图像。

  1. 你可以画出on paint事件
  2. 使用BackgroundImage属性。

答案 2 :(得分:1)

是。 Panel class有一个名为BackImageUrl的成员。只需指定要用作背景的图片的网址。

答案 3 :(得分:1)

对于winforms,您可以使用Panel的BackgroundImageBackgroundImageLayout属性。