【问题标题】:Windows Forms PictureBox - how to display the image in a certain area of the formWindows Forms PictureBox - 如何在窗体的某个区域显示图像
【发布时间】:2013-01-30 13:53:15
【问题描述】:

我正在使用以下代码使用fileDialog 在我的一种表单中打开和显示图像:

private void btnExplorer_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    PictureBox PictureBox1 = new PictureBox();
                    PictureBox1.Image = new Bitmap(openFileDialog1.FileName);
                    // Add the new control to its parent's controls collection
                    this.Controls.Add(PictureBox1);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error loading image" + ex.Message);
                }
            }
        }

问题是我的图像显示在表单的左上角,而我为此目的已经留下了几乎四分之一的右下角。我怎样才能在那里展示它?

【问题讨论】:

标签: c# winforms visual-studio-2010


【解决方案1】:

就像我在评论中所说的那样,方法如下:How to: Position Controls on Windows Forms

PictureBox PictureBox1 = new PictureBox();
PictureBox1.Image = new Bitmap(openFileDialog1.FileName);
PictureBox1.Location = new Point(20, 100); //20 from left and 100 from top
this.Controls.Add(PictureBox1);

或者以后再改:

PictureBox1.Top += 50; //increase distance from top with 50

【讨论】:

    【解决方案2】:

    您可以在将 PictureBox 添加到 Parent 之前设置它的 location 属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-22
      • 1970-01-01
      相关资源
      最近更新 更多