/// <summary> /// Converts an <see cref="Image" /> (or an object that can be cast to an <see cref="Image" />) to the specified type. /// </summary> /// <param name="context">An <see cref="ITypeDescriptorContext" /> that provides a format context. In this converter this parameter is ignored.</param> /// <param name="culture">A <see cref="CultureInfo" />. In this converter this parameter is ignored.</param> /// <param name="value">The <see cref="Image" /> to convert.</param> /// <param name="destinationType">The <see cref="Type" /> to convert the <see cref="Image" /> to. /// This type converter supports <see cref="Array">byte[]</see> type.</param> /// <returns>An <see cref="object" /> that represents the converted value.</returns> public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType != typeof(byte[]) || !(value is Image)) { return(base.ConvertTo(context, culture, value, destinationType)); } // 1.) Metafile: Saving as EMF/WMF (base would save a PNG here) if (value is Metafile metafile) { using (var ms = new MemoryStream()) { metafile.Save(ms); return(ms.ToArray()); } } // 2.) Icon bitmap: Preserving images at least in one color depth var bitmap = (Bitmap)value; if (bitmap.RawFormat.Guid == ImageFormat.Icon.Guid) { Bitmap[] images = bitmap.ExtractIconImages(); using (Icon icon = Icons.Combine(images)) { images.ForEach(i => i.Dispose()); using (var ms = new MemoryStream()) { icon.Save(ms); return(ms.ToArray()); } } } // 3.) TIFF: Saving every page if (bitmap.RawFormat.Guid == ImageFormat.Tiff.Guid) { Bitmap[] images = bitmap.ExtractBitmaps(); using (var ms = new MemoryStream()) { images.SaveAsMultipageTiff(ms); return(ms.ToArray()); } } // 4.) Any other: base works well return(base.ConvertTo(context, culture, value, destinationType)); }