注:问题 Using 256 x 256 Vista icon in application处理使用“Vista”图标作为应用程序的图标。这个问题涉及手动绘制 Vista 图标。
注:问题 WinForms .NET 2.0: How to paint the proper sized icon?处理绘制从文件加载的 Vista 图标。此问题涉及绘制从 .resource 加载的 Vista 图标。
我在我的 Visual Studio 项目中包含了一个具有各种格式的图标:
现在想画256x256的版本。我尝试过的以下任何事情都不起作用。
下面绘制了拉伸(stretch)到 256x256 的 32x32 格式:
Icon ico = Properties.Resources.TestIconThatHasA256PNGFormat;
e.Graphics.DrawIcon(ico, new Rectangle(0, 0, 256, 256));
下面绘制了未拉伸(stretch)的 32x32 格式:
Icon ico = Properties.Resources.TestIconThatHasA256PNGFormat;
e.Graphics.DrawIconUnstretched(ico, new Rectangle(0, 0, 256, 256));
下面绘制了拉伸(stretch)到 256x256 的 32x32 格式:
Icon ico = Properties.Resources.TestIconThatHasA256PNGFormat;
e.Graphics.DrawImage(ico.ToBitmap(), new Rectangle(0, 0, 256, 256));
下面绘制了拉伸(stretch)到 256x256 的 48x48 格式:
Icon ico = Properties.Resources.TestIconThatHasA256PNGFormat;
e.Graphics.DrawIcon(
new Icon(ico, new Size(256, 256)),
new Rectangle(0, 0, 256, 256));
如何绘制 256x256 格式的图标?
笔记:
2 : 问题 WinForms .NET 2.0: How to paint the proper sized icon?
最佳答案
ResourceManager
根据存储在资源中的位加载图标。但是,它处理加载的方式不允许您访问 256x256 图标(此信息不会进入您返回的 System.Drawing.Icon
)。
很抱歉让您失望了,但我知道的唯一有效方法是通过 LoadImage
的 P/Invoke 加载图标并使用文件(是的,我知道,这不是你要找的)。所以新的问题应该是:我如何提取给定资源的位,以便我可以将它们存储到文件中?我担心这也是不可能的,已经完成了一些步骤 System.Resources.ResourceReader
,因为资源数据似乎是序列化的 .NET 对象的集合。
无论如何,对于那些有能力从 .ICO 文件加载图标的人(以及我自己,作为 future 如何加载 256x256 图标的引用),请调用 IconConverter.LoadIcon
:
using System.Runtime.InteropServices;
static class IconConverter
{
public static System.Drawing.Icon LoadIcon(string path, int width, int height)
{
System.IntPtr hIcon;
hIcon = LoadImage (System.IntPtr.Zero, path, IMAGE_ICON, width, height,
LR_LOADFROMFILE);
if (hIcon == System.IntPtr.Zero)
{
return null;
}
return System.Drawing.Icon.FromHandle (hIcon);
}
const int IMAGE_ICON = 1;
const int LR_LOADFROMFILE = 0x0010;
[DllImport ("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]
static extern System.IntPtr LoadImage(System.IntPtr hInstance,
string lpszName, uint uType,
int cxDesired, int cyDesired,
uint fuLoad);
}
一旦您拥有
System.Drawing.Icon
在预期的大小,只需使用 graphics.DrawIconUnstretched
进行绘制.
关于.net - Windows 窗体 .NET 2.0 : How to draw a PNG icon?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/287041/