查找图像路径并使用 PictureBox 查看
Find image path and view using PictureBox
我有一个表单应用程序,我可以在其中输入图像名称并在单击按钮后在 PictureBox
中查看图像。
private void button1_Click(object sender, EventArgs e)
{
string image = textBox1.Text + ".jpg";
PictureBox pictureBox1 = new PictureBox();
pictureBox1.ImageLocation = image;
}
这是我的代码,但它没有执行任何操作,我尝试搜索的图像没有出现在 PictureBox
中。可能会出什么问题?答案将不胜感激。
因为您创建了 PictureBox
的新实例,但您没有将其添加到您的表单中。您应该像这样将其添加到表单的控件中:
string image = textBox1.Text + ".jpg";
PictureBox pictureBox1 = new PictureBox();
//Set pictureBox1's location on the form
pictureBox1.Location = new Point(10 , 10);
//Add pictureBox1 to your form
Controls.Add(pictureBox1);
现在,如果您的 image
变量包含有效的图像路径,您的 PictureBox
应该显示它。
编辑: 要在 TextBox
中写入有效的图片路径,请尝试在 TextBox
中写入完整图片的路径,如下所示:
D:\Pics\yourPic
或者如果你已经将它添加到你的项目中,它应该是这样的:
D:\New folder (1)\WindowsFormsApplication1\WindowsFormsApplication1\yourPic
请不要忘记,如果您已经在表单中放置了 PictureBox
,您只需在代码中调用它即可。您不需要创建一个新的。你应该删除这一行 PictureBox pictureBox1 = new PictureBox();
.
我有一个表单应用程序,我可以在其中输入图像名称并在单击按钮后在 PictureBox
中查看图像。
private void button1_Click(object sender, EventArgs e)
{
string image = textBox1.Text + ".jpg";
PictureBox pictureBox1 = new PictureBox();
pictureBox1.ImageLocation = image;
}
这是我的代码,但它没有执行任何操作,我尝试搜索的图像没有出现在 PictureBox
中。可能会出什么问题?答案将不胜感激。
因为您创建了 PictureBox
的新实例,但您没有将其添加到您的表单中。您应该像这样将其添加到表单的控件中:
string image = textBox1.Text + ".jpg";
PictureBox pictureBox1 = new PictureBox();
//Set pictureBox1's location on the form
pictureBox1.Location = new Point(10 , 10);
//Add pictureBox1 to your form
Controls.Add(pictureBox1);
现在,如果您的 image
变量包含有效的图像路径,您的 PictureBox
应该显示它。
编辑: 要在 TextBox
中写入有效的图片路径,请尝试在 TextBox
中写入完整图片的路径,如下所示:
D:\Pics\yourPic
或者如果你已经将它添加到你的项目中,它应该是这样的:
D:\New folder (1)\WindowsFormsApplication1\WindowsFormsApplication1\yourPic
请不要忘记,如果您已经在表单中放置了 PictureBox
,您只需在代码中调用它即可。您不需要创建一个新的。你应该删除这一行 PictureBox pictureBox1 = new PictureBox();
.