求补全c#代码:我想也要实现单击按钮后从电脑里选择一张图片,然后通过picturebox这个控件显示出来。

2025-05-12 16:56:23
推荐回答(2个)
回答(1):

没这麼复杂!看下我的代码:

private void button1_Click(object sender, EventArgs e)
{
string ImgPath = "";
OpenFileDialog openfiledialog = new OpenFileDialog();//初始化openfiledialog对象
openfiledialog.InitialDirectory = "c:";//设置初始目录
openfiledialog.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*";
if (openfiledialog.ShowDialog() == DialogResult.OK)
{
ImgPath = openfiledialog.FileName;
if (File.Exists(ImgPath))
{
pictureBox1.ImageLocation = ImgPath;
}
}
}

回答(2):

汗,后面你就直接给你的那个 控件下面写个连接代码不就行了?用一个 button空间实现,或者在窗体下直接写代码,