<PackageReference Include="System.Drawing.Common" Version="10.0.0-preview.1.25080.3" />

ToolboxBitmapAttribute

Allows you to specify an icon to represent a control in a container, such as the Microsoft Visual Studio Form Designer.
using System.Diagnostics.CodeAnalysis; using System.Drawing.Imaging; using System.IO; using System.Runtime.CompilerServices; namespace System.Drawing { [NullableContext(2)] [Nullable(0)] [AttributeUsage(AttributeTargets.Class)] public class ToolboxBitmapAttribute : Attribute { private Image _smallImage; private Image _largeImage; private static readonly Size s_largeSize; private static readonly Size s_smallSize; [Nullable(1)] public static readonly ToolboxBitmapAttribute Default; [Nullable(1)] private static readonly ToolboxBitmapAttribute s_defaultComponent; [NullableContext(1)] public ToolboxBitmapAttribute(string imageFile) : this(GetImageFromFile(imageFile, false, true), GetImageFromFile(imageFile, true, true)) { } [NullableContext(1)] public ToolboxBitmapAttribute(Type t) : this(GetImageFromResource(t, null, false), GetImageFromResource(t, null, true)) { } [NullableContext(1)] public ToolboxBitmapAttribute(Type t, string name) : this(GetImageFromResource(t, name, false), GetImageFromResource(t, name, true)) { } private ToolboxBitmapAttribute(Image smallImage, Image largeImage) { _smallImage = smallImage; _largeImage = largeImage; } public override bool Equals([NotNullWhen(true)] object value) { if (value == this) return true; ToolboxBitmapAttribute toolboxBitmapAttribute = value as ToolboxBitmapAttribute; if (toolboxBitmapAttribute != null) { if (toolboxBitmapAttribute._smallImage == _smallImage) return toolboxBitmapAttribute._largeImage == _largeImage; return false; } return false; } public override int GetHashCode() { return base.GetHashCode(); } public Image GetImage(object component) { return GetImage(component, true); } public Image GetImage(object component, bool large) { if (component == null) return null; return GetImage(component.GetType(), large); } [NullableContext(1)] [return: Nullable(2)] public Image GetImage(Type type) { return GetImage(type, false); } [NullableContext(1)] [return: Nullable(2)] public Image GetImage(Type type, bool large) { return GetImage(type, null, large); } public Image GetImage([Nullable(1)] Type type, string imgName, bool large) { if ((large && _largeImage == null) || (!large && _smallImage == null)) { Image image = large ? _largeImage : _smallImage; if (image == null) image = GetImageFromResource(type, imgName, large); if (large && _largeImage == null && _smallImage != null) image = new Bitmap((Bitmap)_smallImage, s_largeSize.Width, s_largeSize.Height); Bitmap bitmap = image as Bitmap; if (bitmap != null) MakeBackgroundAlphaZero(bitmap); if (image == null) { image = s_defaultComponent.GetImage(type, large); if (image != null) image = (Image)image.Clone(); } if (large) _largeImage = image; else _smallImage = image; } Image result = large ? _largeImage : _smallImage; if (Equals(Default)) { _largeImage = null; _smallImage = null; } return result; } private static Bitmap GetIconFromStream(Stream stream, bool large, bool scaled) { if (stream != null) { Bitmap logicalBitmap = null; try { using (Icon original = new Icon(stream)) using (Icon icon = new Icon(original, large ? s_largeSize : s_smallSize)) { logicalBitmap = icon.ToBitmap(); if (!(DpiHelper.IsScalingRequired & scaled)) return logicalBitmap; DpiHelper.ScaleBitmapLogicalToDevice(ref logicalBitmap); return logicalBitmap; } } catch (Exception ex) when (!ClientUtils.IsCriticalException(ex)) { return logicalBitmap; } } return null; } private static Image GetImageFromFile(string imageFile, bool large, bool scaled = true) { Image image = null; try { if (imageFile == null) return image; string extension = Path.GetExtension(imageFile); if (extension != null && string.Equals(extension, ".ico", StringComparison.OrdinalIgnoreCase)) { using (FileStream stream = File.OpenRead(imageFile)) { image = GetIconFromStream(stream, large, scaled); return image; } } if (large) return image; image = Image.FromFile(imageFile); Bitmap logicalBitmap = image as Bitmap; if (!(DpiHelper.IsScalingRequired & scaled)) return image; DpiHelper.ScaleBitmapLogicalToDevice(ref logicalBitmap); return image; } catch (Exception ex) when (!ClientUtils.IsCriticalException(ex)) { return image; } } private static Image GetBitmapFromResource([Nullable(1)] Type t, string bitmapName, bool large, bool scaled) { if (bitmapName != null) { Image image = null; try { Stream resourceStream = BitmapSelector.GetResourceStream(t, bitmapName); if (resourceStream == null) return image; Bitmap logicalBitmap = new Bitmap(resourceStream); image = logicalBitmap; MakeBackgroundAlphaZero(logicalBitmap); if (large) image = new Bitmap(logicalBitmap, s_largeSize.Width, s_largeSize.Height); if (!(DpiHelper.IsScalingRequired & scaled)) return image; logicalBitmap = (Bitmap)image; DpiHelper.ScaleBitmapLogicalToDevice(ref logicalBitmap); image = logicalBitmap; return image; } catch (Exception ex) when (!ClientUtils.IsCriticalException(ex)) { return image; } } return null; } private static Bitmap GetIconFromResource([Nullable(1)] Type t, string bitmapName, bool large, bool scaled) { if (bitmapName != null) return GetIconFromStream(BitmapSelector.GetResourceStream(t, bitmapName), large, scaled); return null; } public static Image GetImageFromResource([Nullable(1)] Type t, string imageName, bool large) { return GetImageFromResource(t, imageName, large, true); } internal static Image GetImageFromResource([Nullable(1)] Type t, string imageName, bool large, bool scaled) { Image image = null; try { string text = null; string text2 = null; string text3 = null; if (imageName == null) { string text4 = t.FullName; int num = text4.LastIndexOf('.'); if (num != -1) { string text5 = text4; int num2 = num + 1; text4 = text5.Substring(num2, text5.Length - num2); } text3 = text4; text = text4 + ".ico"; text2 = text4 + ".bmp"; } else if (string.Equals(Path.GetExtension(imageName), ".ico", StringComparison.CurrentCultureIgnoreCase)) { text = imageName; } else if (string.Equals(Path.GetExtension(imageName), ".bmp", StringComparison.CurrentCultureIgnoreCase)) { text2 = imageName; } else { text3 = imageName; text2 = imageName + ".bmp"; text = imageName + ".ico"; } if (text3 != null) image = GetBitmapFromResource(t, text3, large, scaled); if (image == null && text3 != null) image = GetIconFromResource(t, text3, large, scaled); if (image == null && text2 != null) image = GetBitmapFromResource(t, text2, large, scaled); if (image != null) return image; if (text == null) return image; image = GetIconFromResource(t, text, large, scaled); return image; } catch (Exception) { return image; } } [NullableContext(1)] private static void MakeBackgroundAlphaZero(Bitmap img) { if (!(img.RawFormat.Guid == ImageFormat.Icon.Guid)) { Color pixel = img.GetPixel(0, img.Height - 1); img.MakeTransparent(); Color color = Color.FromArgb(0, pixel); img.SetPixel(0, img.Height - 1, color); } } static ToolboxBitmapAttribute() { s_largeSize = new Size(32, 32); s_smallSize = new Size(16, 16); Default = new ToolboxBitmapAttribute((Image)null, (Image)null); Bitmap bitmap = new Bitmap(BitmapSelector.GetResourceStream(typeof(ToolboxBitmapAttribute), "DefaultComponent.bmp")); MakeBackgroundAlphaZero(bitmap); s_defaultComponent = new ToolboxBitmapAttribute(bitmap, null); } } }