C# Windows 窗体 PictureBox - 如何在窗体的某个区域显示图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14605687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows Forms PictureBox - how to display the image in a certain area of the form
提问by Leron_says_get_back_Monica
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?
问题是我的图像显示在表单的左上角,为此我已经离开了右下角的近四分之一。我怎样才能在那里显示它?
采纳答案by Abbas
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
回答by Gian Acuna
You can set the location property of the PictureBox before adding it to the Parent.
您可以在将 PictureBox 添加到 Parent 之前设置它的 location 属性。