<PackageReference Include="System.Drawing.Common" Version="10.0.0-rc.2.25502.107" />

IconConverter

Converts an Icon object from one data type to another. Access this class through the TypeDescriptor object.
using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.IO; using System.Runtime.CompilerServices; namespace System.Drawing { [NullableContext(2)] [Nullable(0)] public class IconConverter : ExpandableObjectConverter { [NullableContext(1)] public override bool CanConvertFrom([Nullable(2)] ITypeDescriptorContext context, Type sourceType) { return sourceType == typeof(byte[]); } public override bool CanConvertTo(ITypeDescriptorContext context, [NotNullWhen(true)] Type destinationType) { if (!(destinationType == typeof(byte[])) && !(destinationType == typeof(string)) && !(destinationType == typeof(Image))) return destinationType == typeof(Bitmap); return true; } public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, [Nullable(1)] object value) { byte[] array = value as byte[]; if (array == null) return base.ConvertFrom(context, culture, value); return new Icon(new MemoryStream(array)); } public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, [Nullable(1)] Type destinationType) { if (destinationType == typeof(string)) { if (value == null) return System.SR.none; if (value is Icon) return value.ToString(); } else if (destinationType == typeof(byte[])) { Icon icon = value as Icon; if (icon != null) { using (MemoryStream memoryStream = new MemoryStream()) { icon.Save(memoryStream); return memoryStream.ToArray(); } } } else if (destinationType == typeof(Image) || destinationType == typeof(Bitmap)) { Icon icon2 = value as Icon; if (icon2 != null) return icon2.ToBitmap(); } throw GetConvertFromException(value); } } }