You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Skia报错‘Failed to create Image decoder unimplemented’问题求助

解决SkiaSharp裁剪后图片字节数组展示时的unimplemented解码错误

问题根源

你直接通过SKBitmap.Bytes获取的是原始RGBA像素裸数据,而非标准图片格式(如PNG/JPG)的编码字节流。ByteArrayToImageSourceConverter和CachedImage无法解码裸像素数据,因此触发Skia的unimplemented错误;同时SKBitmap资源未手动释放,导致A resource failed to call close警告。

解决方案

  1. 将SKBitmap编码为标准图片格式的字节数组,而非直接读取原始像素数据
  2. 手动释放SKBitmap及相关资源,避免泄漏

代码修改

1. 修正裁剪页面的图片字节数组生成逻辑

替换原SetImages方法中设置AppManager.ProfileImage的代码:

public async void SetImages()
{
    try
    {
        if (isProfileImage)
        {
            foreach (CutImages cutImage in ImageObjects)
            {
                ProfileImage = cutImage.CroppedImage.Resize(new SKImageInfo(360, 360), SKFilterQuality.High);
            }
            
            // 将Bitmap编码为PNG格式的字节数组(也可改用SKEncodedImageFormat.Jpeg)
            using (SKImage image = SKImage.FromBitmap(ProfileImage))
            using (SKData data = image.Encode(SKEncodedImageFormat.Png, 100))
            {
                AppManager.ProfileImage = data.ToArray();
            }
            
            // 手动释放Bitmap资源
            ProfileImage.Dispose();
            
            await NavigationService.GoBackAsync();
            ImageObjects.Clear();
            return;
        }
        // 其余代码保持不变
    }
    catch (Exception ex) 
    { 
        Console.WriteLine(ex);
    }
}

2. 修正属性监听的名称匹配问题

原监听代码中判断的属性名CroppedImage与实际设置的ProfileImage不匹配,需修正:

public override void AppManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    base.AppManager_PropertyChanged(sender, e);
    if(e.PropertyName == nameof(AppManager.ProfileImage))
    {
        ProfileImage = AppManager.ProfileImage;
    }
}

说明

  • SKImage.FromBitmap将SKBitmap转换为可编码的图像对象,Encode方法生成标准格式的字节流,确保转换器能正确解码
  • 使用using语句自动释放SKImage、SKData资源,手动Dispose SKBitmap,解决资源未关闭的警告

内容的提问来源于stack exchange,提问作者WoistdasNiveau

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 17:35:27