c#表单-加载图像
本文关键字:图像 加载 表单 | 更新日期: 2023-09-27 18:01:29
我正在尝试加载一个特定的图像在我的图片框如果一个特定的单选按钮被选中。
运行时错误是"_cannot locate file in this directory.",所以我把图像移到那个目录,但这只会导致图片框自动加载它。我还导入了所有需要的图像。
private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked)
{
pictureBox1.Load("10C.jpg");
}
}
当我运行程序时,pictureBox1
已经加载了图像。我想要我的pictureBox1
做的是保持空白,直到用户选择一个单选按钮并单击"更改图像"按钮。
您应该将pictureBox1.Visible = false;
放置在表单加载事件
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Visible = false;
}
您可以使pictureBox1。Visible = false;然后你可以说:
private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked)
{
pictureBox1.Visible = true;
pictureBox1.Load("10C.jpg");
}
}
这样,在点击按钮之前,图像是看不到的。
我建议您在启动时在图片框中设置默认图像,如果选中单选按钮并单击按钮,则将desired image
(10C.jpg)加载到图片框中,而不是处理图片框的visible
属性。
代码看起来像:
private void button1_Click(object sender, EventArgs e)
{
// pictureBox1.Visible will be always set to true
if (radioButton1.Checked)
{
pictureBox1.Load("10C.jpg");
}
else
{
pictureBox1.Load("placeholder.jpg");
}
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Visible = true;
pictureBox1.Load("placeholder.jpg");
}
建议:搜索谷歌placeholder image
希望有帮助!
编辑
响应:
运行时错误是" _cannot locate file in this directory.",所以我把图片移到了那个目录,但那只会导致图片框自动加载。我还导入了所有的我需要的图片。
在配置文件中定义一个键,它包含一个图像路径,并通过在末尾连接文件名来访问您的图像。使用这种方法,您甚至可以在部署之后更改映像路径。