我必须将ImageList中的图像添加到带有路径的资源文件中的按钮中。我试过下面的代码:
private void AddMyImage()
{
//assign image to imagelist
imgList.Images.Add(Image.FromFile(@"Resources\SoccerBall.jpg"));
//Assign the ImageList to the button control.
button1.ImageList = imgList;
// Select the image from the ImageList (using the ImageIndex property).
button1.ImageIndex = 0;
imgList.ImageSize = new Size(120, 120);
}
private void Form1_Load(object sender, EventArgs e)
{
AddMyImage();
}我在运行时收到一个ExceptionUnhandled错误,显示为System.IO.FileNotFoundException:'...\Resources\SoccerBall.jpg‘
资源路径是什么?
发布于 2019-09-27 16:30:52
试试这个:
var imageStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("WindowsFormsApp1.Resources.SoccerBall.jpg");
if(imageStream != null)
{
var image = Image.FromStream(imageStream);
imgList.Images.Add(image);
} 其中WindowsFormsApp1应该是应用程序的名称空间。注意:确保您的图像设置为Embedded Resource作为Visual Studio中的build操作(在解决方案资源管理器中单击图像,对于属性set Build action to Embedded Resource)。
https://stackoverflow.com/questions/58129999
复制相似问题