【问题标题】:How Do I Create a System.Drawing.Icon with Multiple Sizes/Images?如何创建具有多种尺寸/图像的 System.Drawing.Icon?
【发布时间】:2010-02-01 03:10:49
【问题描述】:

我想通过 32x32、16x16 位图以编程方式创建单个 System.Drawing.Icon。这可能吗?如果我用 -

加载图标
Icon myIcon = new Icon(@"C:\myIcon.ico");

...它可以包含多个图像。

【问题讨论】:

    标签: c# .net winapi icons


    【解决方案1】:

    一个 .ico 文件 中可以有多个图像,但是当您加载一个 .ico 文件并创建一个 Icon 对象时,只会加载其中一个图像。 Windows 会根据当前的显示模式和系统设置选择最合适的图像,并使用该图像来初始化 System.Drawing.Icon 对象,而忽略其他图像。

    所以你不能创建一个包含多个图像的System.Drawing.Icon,你必须在创建Icon 对象之前选择一个。

    当然,可以在运行时从位图创建图标,这是您真正要问的吗?或者您是在问如何创建 .ico 文件?

    【讨论】:

    • 但是如果我像上面显示的那样加载 .ico 文件,我可以像这样访问 16x16、32x32、48x48 等 -: Icon smallIcon = new Icon(myIcon, 16, 16) ;
    • 因为它知道图标是从哪个文件/资源​​创建的,并且可以返回到它并获取新图像。但是,无法将图像添加到 Icon 对象。
    • @JohnKnoeller 实际上,这不是真的。所有图像数据都由Icon 类加载到内存中,当您从另一个Icon 对象调整大小时,图像数据是共享的,并且从原始(共享)数据中提取最接近的大小。
    【解决方案2】:

    您可以尝试使用png2ico 创建一个.ico 文件,并使用System.Diagnostics.Process.Start 调用它。在调用 png2ico 之前,您需要创建图像并将其保存到光盘。

    【讨论】:

      【解决方案3】:

      有一个很好的code snippet here。它使用Icon.FromHandle 方法。

      来自链接:

      /// <summary>
      /// Converts an image into an icon.
      /// </summary>
      /// <param name="img">The image that shall become an icon</param>
      /// <param name="size">The width and height of the icon. Standard
      /// sizes are 16x16, 32x32, 48x48, 64x64.</param>
      /// <param name="keepAspectRatio">Whether the image should be squashed into a
      /// square or whether whitespace should be put around it.</param>
      /// <returns>An icon!!</returns>
      private Icon MakeIcon(Image img, int size, bool keepAspectRatio) {
        Bitmap square = new Bitmap(size, size); // create new bitmap
        Graphics g = Graphics.FromImage(square); // allow drawing to it
      
        int x, y, w, h; // dimensions for new image
      
        if(!keepAspectRatio || img.Height == img.Width) {
          // just fill the square
          x = y = 0; // set x and y to 0
          w = h = size; // set width and height to size
        } else {
          // work out the aspect ratio
          float r = (float)img.Width / (float)img.Height;
      
          // set dimensions accordingly to fit inside size^2 square
          if(r > 1) { // w is bigger, so divide h by r
            w = size;
            h = (int)((float)size / r);
            x = 0; y = (size - h) / 2; // center the image
          } else { // h is bigger, so multiply w by r
            w = (int)((float)size * r);
            h = size;
            y = 0; x = (size - w) / 2; // center the image
          }
        }
      
        // make the image shrink nicely by using HighQualityBicubic mode
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        g.DrawImage(img, x, y, w, h); // draw image with specified dimensions
        g.Flush(); // make sure all drawing operations complete before we get the icon
      
        // following line would work directly on any image, but then
        // it wouldn't look as nice.
        return Icon.FromHandle(square.GetHicon());
      } 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-05
        • 1970-01-01
        • 2023-01-26
        • 2019-01-18
        • 1970-01-01
        相关资源
        最近更新 更多