C# 如何更改 PictureBox 的图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11487901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I change a PictureBox's image?
提问by Steven
I have a program in C# with a PictureBoxobject 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#程序与PictureBox内部的对象Form。我如何更改它的图片?可供选择的图片在bin/Pics中;如果这很重要,它们是 jpeg 格式。
采纳答案by Steven
Assign a new Imageobject to your PictureBox's Imageproperty. To load an Imagefrom a file, you may use the Image.FromFilemethod. 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文件加载,您可以使用该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.
此外,如果这些图像是静态的并且仅用作应用程序中的资源,那么资源将比文件更适合。
回答by mms
You can use the ImageLocationproperty of pictureBox1:
您可以使用以下ImageLocation属性pictureBox1:
pictureBox1.ImageLocation = @"C:\Users\MSI\Desktop\MYAPP\Slider\Slider\bt1.jpg";

