3

我正在使用以下代码以我的一种形式打开和显示图像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);
                }
            }
        }

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

4

2 回答 2

8

就像我在评论中所说的那样,方法如下:如何:在 Windows 窗体上定位控件

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
于 2013-01-30T14:05:04.327 回答
1

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

于 2013-01-30T13:57:24.193 回答