首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >链接imagelist中嵌入的资源项

链接imagelist中嵌入的资源项
EN

Stack Overflow用户
提问于 2019-09-27 15:41:03
回答 1查看 233关注 0票数 0

我必须将ImageList中的图像添加到带有路径的资源文件中的按钮中。我试过下面的代码:

代码语言:javascript
复制
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‘

资源路径是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-27 16:30:52

试试这个:

代码语言:javascript
复制
    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)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58129999

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档