简体   繁体   English

Windows窗体PictureBox - 如何在窗体的某个区域中显示图像

[英]Windows Forms PictureBox - how to display the image in a certain area of the form

I'm using the following code to open and display image in one of my forms using fileDialog : 我正在使用以下代码使用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);
                }
            }
        }

The problem is that my image is shown at the top left corner of my form, when I have left almost quarter of my down-right side for this purpose. 问题是我的图像显示在我的表单的左上角,当我为此目的离开了几乎四分之一的右下角时。 How can I show it there? 我怎么能在那里展示它?

Like I said in my comment, here's how: How to: Position Controls on Windows Forms . 就像我在评论中所说,这是如何: 如何:在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);

Or change it afterwards: 或者之后改变它:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM