如何让任务栏中的.ico格式图标透明显示?

How to make .ico format icon in task bar appear transparent?

我希望 windows 表单应用程序打开时出现的任务栏图标的背景是透明的。但是,该图标出现在任务栏中时背景为白色。我打开了 .ico 文件,它有表示透明度的方格背景。

如何使任务栏中图标的背景透明?

这是我第一次向 windows 表单应用程序添加图标。我也尝试过使用 .png 文件,但任务栏中显示的只是默认的 .png 图标。

这是在 class 范围内声明对象图标的代码:

Icon icon = Icon.ExtractAssociatedIcon("galaxyicon.ico");

我在每个Form_Load方法中使用下面的代码将图标设置为任务栏中的图标对象。

this.Icon = icon; 

我希望在这种情况下使用透明图标,但得到的却是白色背景。

你应该直接加载图标文件using new Icon(fileName)

您需要定义图标的 "transparent" 颜色,例如:

//using System.Drawing;

#region MakeTransparentIcon

    ///<summary>
    /// Manipulates the background of an Icon
    ///</summary>
    ///<param name="icon">Icon source</param>
    ///<param name="disposeIcon">Icon dispose</param>
    ///<returns><see cref="Icon"/> or <see cref="T:null"/></returns>
    public static Icon MakeTransparentIcon(Icon icon, bool disposeIcon = true)
    {
        if (icon != null)
        {
            using (Bitmap bm = icon.ToBitmap())
            {
                bm.MakeTransparent(Color.Transparent); // define the background as transparent
                                                       // you need to align the color to your needs
                if (disposeIcon)
                {
                    icon.Dispose();
                }
                return Icon.FromHandle(bm.GetHicon());
            }
        }
        return null;
    }
    #endregion