gpt4 book ai didi

c# - Windows 窗体 PictureBox - 如何在窗体的特定区域显示图像

转载 作者:行者123 更新时间:2023-11-30 13:47:57 25 4
gpt4 key购买 nike

我正在使用以下代码使用 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);
}
}
}

问题是我的图像显示在表单的左上角,为此我将右下角的四分之一留了下来。我怎样才能在那里显示它?

最佳答案

正如我在评论中所说,方法如下: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

关于c# - Windows 窗体 PictureBox - 如何在窗体的特定区域显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14605687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com