Windows 窗体图片框 - 如何在窗体的某个区域中显示图像
本文关键字:窗体 区域 显示 图像 显示图 Windows | 更新日期: 2023-09-27 17:56:35
我使用以下代码通过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);
}
}
}
问题是我的图像显示在表单的左上角,而我为此目的留下了几乎四分之一的右下角。我怎样才能在那里展示它?
就像我在评论中所说的那样,方法如下:如何:在 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
可以在将
图片框添加到父级之前设置图片框的位置属性。