我必须将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'
资源路径是什么?
答案 0 :(得分:0)
尝试一下:
var imageStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("WindowsFormsApp1.Resources.SoccerBall.jpg");
if(imageStream != null)
{
var image = Image.FromStream(imageStream);
imgList.Images.Add(image);
}
WindowsFormsApp1应该是应用程序的名称空间。注意:确保在Visual Studio中将映像设置为“嵌入式资源”作为生成操作(在解决方案资源管理器中单击该图像,并将“生成操作”设置为“嵌入式资源”的属性)。
答案 1 :(得分:0)