C#下读取Resource的png图片并放入imagelist中

string address_temp = AppDomain.CurrentDomain.BaseDirectory ;
string address = address_temp.Substring(0,address_temp.Length - 11);
string[] image1 = Directory.GetFiles(address+@"\Resources", "*.png");

ImageList image = new ImageList(); ;
List<string> st=new List<string>();
System.Resources.ResourceManager rm= YQJDESIGN.Properties.Resources.ResourceManager;
int i=0;
foreach (var mp in image1)
{
//string temp_st;

st.Add (mp.Replace(address+"\\Resources\\", "").Replace(".png",""));
Image obj = (System.Drawing.Image)rm.GetObject(st[i], YQJDESIGN.Properties.Resources.Culture);
image.Images.Add(obj);
i++;
}

转载于:https://www.cnblogs.com/jiangben/archive/2013/05/21/3091163.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过以下步骤来实现: 1. 读取图片文件夹的所有图片文件,可以使用 `Directory.GetFiles` 方法,将文件路径存储到一个字符串数组。 2. 创建一个 `ImageList` 控件,将读取到的每个图片文件加载到 `ImageList` ,可以使用 `Image.FromFile` 方法。 3. 创建一个 `ComboBox` 控件,将 `ImageList` 图片绑定到 `ComboBox` 的 `DropDownList` 属性上,可以使用 `ComboBox.DrawMode` 属性设置为 `DrawMode.OwnerDrawFixed`,然后在 `ComboBox.DrawItem` 事件绘制每个图片项。 以下是示例代码: ```csharp private void Form1_Load(object sender, EventArgs e) { string[] imageFiles = Directory.GetFiles(@"C:\Images"); ImageList imageList = new ImageList(); foreach (string file in imageFiles) { imageList.Images.Add(Image.FromFile(file)); } comboBox1.DrawMode = DrawMode.OwnerDrawFixed; comboBox1.DropDownStyle = ComboBoxStyle.DropDownList; comboBox1.DataSource = imageFiles; comboBox1.ItemHeight = imageList.ImageSize.Height + 2; comboBox1.DropDownWidth = imageList.ImageSize.Width + SystemInformation.VerticalScrollBarWidth; comboBox1.DrawItem += ComboBox1_DrawItem; } private void ComboBox1_DrawItem(object sender, DrawItemEventArgs e) { if (e.Index < 0) { return; } ComboBox comboBox = (ComboBox)sender; string file = (string)comboBox.Items[e.Index]; ImageList imageList = comboBox.DataSource as ImageList; if (imageList != null) { Image image = imageList.Images[e.Index]; e.Graphics.DrawImage(image, e.Bounds.Left + 1, e.Bounds.Top + 1, imageList.ImageSize.Width, imageList.ImageSize.Height); } else { e.Graphics.DrawString(file, comboBox.Font, Brushes.Black, e.Bounds.Left + 1, e.Bounds.Top + 1); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值