在面板上显示图像

Lou*_*isL 5 .net c# image panel

我的代码的这个ia片段....

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


    }
Run Code Online (Sandbox Code Playgroud)

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

Gun*_*arJ 11

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


Jav*_*ram 5

基本上PictureBox是由以显示图像,同时Panel用于绘制在它(曲线,直线,矩形,....)

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

  1. 您可以绘制它on paint事件
  2. 使用BackgroundImage财产。