Convert System.Drawing.Icon to SkiaSharp.SKBitmap

2.7k views Asked by At

I need to get the Icon of an Executable as SKBitmap I already know how I can get the Icon of the Executable, but I'm stuck finding a way to convert the System.Drawing.Icon to a SkiaSharp.SKBitmap which the Project I'm working on uses to apply further Processing. I am aware of the Icon.toBitmap() which gives me a System.Drawing.Bitmap, but i can't convert that either.

    Icon icon = Icon.ExtractAssociatedIcon(/*Path to FooBar.exe*/);
    SKBitmap skbm = /* what goes here ? */;
2

There are 2 answers

0
Felix Lehmann On BEST ANSWER

The Following solves the Problem

using MemoryStream mem = new MemoryStream();
Icon.ExtractAssociatedIcon(ProgramLocation).Save(mem);
mem.Seek(0, SeekOrigin.Begin);
SKBitmap skbm = SKBitmap.Decode(mem);
mem.Close();
2
Vurdalakov On
using System.Drawing;
using SkiaSharp;

/.../

Icon icon = Icon.ExtractAssociatedIcon(applicationPath);
SKBitmap skBitmap = icon.ToSKBitmap();

/... or (original answer) .../

Icon icon = Icon.ExtractAssociatedIcon(applicationPath);
SKBitmap skBitmap = icon.ToBitmap().ToSKBitmap();

/.../

namespace Vurdalakov
{
    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;
    using SkiaSharp;

    public static class BitmapExtensions
    {
        public static SKBitmap ToSKBitmap(this Bitmap bitmap)
        {
            using (var stream = new MemoryStream())
            {
                bitmap.Save(stream, ImageFormat.Png);

                stream.Seek(0, SeekOrigin.Begin);

                return SKBitmap.Decode(stream);
            }
        }

        public static SKBitmap ToSKBitmap(this Icon icon)
        {
            using (var stream = new MemoryStream())
            {
                icon.Save(stream);

                stream.Seek(0, SeekOrigin.Begin);

                return SKBitmap.Decode(stream);
            }
        }
    }
}