【问题标题】:Convert System.Drawing.Icon to SkiaSharp.SKBitmap将 System.Drawing.Icon 转换为 SkiaSharp.SKBitmap
【发布时间】:2021-02-04 15:38:05
【问题描述】:

我需要得到一个可执行文件的图标为SKBitmap 我已经知道如何获得可执行文件的图标,但我一直在寻找一种将System.Drawing.Icon 转换为SkiaSharp.SKBitmap 的方法,我正在从事的项目用于应用进一步的处理。我知道Icon.toBitmap() 给了我System.Drawing.Bitmap,但我也无法转换它。

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

【问题讨论】:

  • nvm,我自己解决了。

标签: c# .net type-conversion icons skiasharp


【解决方案1】:
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);
            }
        }
    }
}

【讨论】:

  • 位图没有ToSKBitmap
  • 没错,我忘了添加我使用的扩展方法。答案已修复。我还添加了一个新的扩展方法(基于您的解决方案),直接将 Icon 转换为 SKBitmap。
【解决方案2】:

以下解决问题

using MemoryStream mem = new MemoryStream();
Icon.ExtractAssociatedIcon(ProgramLocation).Save(mem);
mem.Seek(0, SeekOrigin.Begin);
SKBitmap skbm = SKBitmap.Decode(mem);
mem.Close();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-10
    • 1970-01-01
    • 2012-02-14
    • 1970-01-01
    • 2019-05-15
    • 1970-01-01
    • 2011-06-21
    相关资源
    最近更新 更多