如何更改PictureBox的图像?

时间:2022-10-28 00:26:45

I have a program in C# with a PictureBox object inside a Form. How do I change its picture? The pictures to chose from are in bin/Pics; they are jpeg in format, if that matters..

我在C#中有一个程序,在Form中有一个PictureBox对象。我该如何改变它的图片?可供选择的图片在bin / Pics中;它们是jpeg的格式,如果这很重要..

1 个解决方案

#1


30  

Assign a new Image object to your PictureBox's Image property. To load an Image from a file, you may use the Image.FromFile method. In your particular case, assuming the current directory is one under bin, this should load the image bin/Pics/image1.jpg, for example:

将新的Image对象分配给PictureBox的Image属性。要从文件加载图像,您可以使用Image.FromFile方法。在您的特定情况下,假设当前目录是bin下的一个,这应该加载图像bin / Pics / image1.jpg,例如:

pictureBox1.Image = Image.FromFile("../Pics/image1.jpg");

Additionally, if these images are static and to be used only as resources in your application, resources would be a much better fit than files.

此外,如果这些图像是静态的并且仅用作应用程序中的资源,则资源将比文件更适合。

#1


30  

Assign a new Image object to your PictureBox's Image property. To load an Image from a file, you may use the Image.FromFile method. In your particular case, assuming the current directory is one under bin, this should load the image bin/Pics/image1.jpg, for example:

将新的Image对象分配给PictureBox的Image属性。要从文件加载图像,您可以使用Image.FromFile方法。在您的特定情况下,假设当前目录是bin下的一个,这应该加载图像bin / Pics / image1.jpg,例如:

pictureBox1.Image = Image.FromFile("../Pics/image1.jpg");

Additionally, if these images are static and to be used only as resources in your application, resources would be a much better fit than files.

此外,如果这些图像是静态的并且仅用作应用程序中的资源,则资源将比文件更适合。