选择System.Drawing.Icon的大小? [英] Selecting the size of a System.Drawing.Icon?
问题描述
我有有几个不同尺寸(16像素,32PX,64PX)图标。我打电话 ToBitmap()
就可以了,但它始终是返回32PX形象。我如何获取64PX吗?
I have a icon which has a few different sizes (16px, 32px, 64px). I am calling ToBitmap()
on it, but it is always returning the 32px image. How do I retrieve the 64px one?
推荐答案
这是ResourceManager类一个相当痛苦的限制。它的getObject()方法不提供一种方式来传递额外的参数,允许按大小选择返回的图标。一种解决方法是将图标添加到项目中来代替。使用Project +添加现有项目,选择您的.ico文件。选择添加的图标,并更改生成操作属性设置为嵌入的资源。
This is a fairly painful limitation in the ResourceManager class. Its GetObject() method doesn't provide a way to pass extra arguments that would allow selecting the returned icon by size. A workaround is to add the icon to the project instead. Use Project + Add Existing Item, select your .ico file. Select the added icon and change the Build Action property to "Embedded Resource".
您现在可以检索与code这样所需的图标:
You can now retrieve the desired icon with code like this:
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");
}
使用范例:
var icon1 = GetIconFromEmbeddedResource("ARW04LT", new Size(16, 16));
var icon2 = GetIconFromEmbeddedResource("ARW04LT", new Size(32, 32));
谨防一种可能的故障模式:该code假定图标添加到包含方法相同的程序集
Beware one possible failure mode: this code assumes that the icon was added to the same assembly that contains the method.
这篇关于选择System.Drawing.Icon的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!