.net - Windows 窗体 .NET 2.0 : How to draw a PNG icon?

标签 .net winforms graphics windows-vista

注:问题 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 项目中包含了一个具有各种格式的图标:

  • 16x16
  • 32x32
  • 48x48
  • 256x256(PNG 压缩)

  • 现在想画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 格式的图标?

    笔记:
  • 图标不是来自文件,所以 PInvoking LoadImage() 将无济于事。
  • 图标不是与文件关联的图标,所以 PInvoking SHGetFileInfo() will没有帮助。 using Icon.ExtractAssociatedIcon 也不会.
  • 我也不想在运行时创作 256x256 格式的图标,所以 libraries designed to do that不会有帮助的。

    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/

    相关文章:

    c# - 保持演示者引用处于事件状态,而不提供对其控制的 View 的引用

    Java Graphics 在没有扩展的情况下不执行 Paint 函数

    Haskell:OpenGL,如何防止窗口立即关闭

    c# - Process.start:如何获得输出?

    .net - 使用 VS 解决方案的 git 子模块时如何解析 nuget 包

    .net - VS2005配置“fallback” DLL

    c# - 不可调用成员 'System.Windows.Forms.Control.Text' 不能像方法一样使用。在 C# 中

    c# - 写入工作表导致 "name already taken"

    algorithm - 匹配颜色的最佳算法。

    c# - 如何解决 AddJsonOptions 不包含 SerializerSettings - .NET 的定义