And*_*Hin 23 c# size icons system.drawing
我有一个图标,有几个不同的尺寸(16px,32px,64px).我正在呼唤ToBitmap()它,但它总是返回32px图像.如何检索64px?
小智 31
这有帮助吗?
Icon sizedIcon = new Icon(Resources.ResourceIcon, new Size(64,64));
Run Code Online (Sandbox Code Playgroud)
Net*_*led 23
对于其他遇到同样问题的人,我找到了一个很好的小解决方案.
Image img = new Icon(Properties.Resources.myIcon, width, height).ToBitmap()
Run Code Online (Sandbox Code Playgroud)
Han*_*ant 13
这是ResourceManager类中相当痛苦的限制.它的GetObject()方法没有提供传递额外参数的方法,这些参数允许按大小选择返回的图标.解决方法是将图标添加到项目中.使用Project + Add Existing Item,选择.ico文件.选择添加的图标,然后将Build Action属性更改为"Embedded Resource".
您现在可以使用以下代码检索所需的图标:
public static Icon GetIconFromEmbeddedResource(string name, Size size) {
var asm = System.Reflection.Assembly.GetExecutingAssembly();
var rnames = asm.GetManifestResourceNames();
var tofind = "." + name + ".ICO";
foreach (string rname in rnames) {
if (rname.EndsWith(tofind, StringComparison.CurrentCultureIgnoreCase)) {
using (var stream = asm.GetManifestResourceStream(rname)) {
return new Icon(stream, size);
}
}
}
throw new ArgumentException("Icon not found");
}
Run Code Online (Sandbox Code Playgroud)
样品用法:
var icon1 = GetIconFromEmbeddedResource("ARW04LT", new Size(16, 16));
var icon2 = GetIconFromEmbeddedResource("ARW04LT", new Size(32, 32));
Run Code Online (Sandbox Code Playgroud)
请注意一种可能的故障模式:此代码假定图标已添加到包含该方法的同一程序集中.
| 归档时间: |
|
| 查看次数: |
19382 次 |
| 最近记录: |