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

Unity中使用.NET实现WebP转PNG的可行方法求助

问题描述

我有通过Base64字符串解码得到的WebP图像字节数组,但Unity不支持WebP格式。已将Unity默认的.NET 2.1切换为.NET Framework 4.x,尝试用System.Drawing和System.Drawing.Imaging库转换格式,但多次尝试均失败:

失败尝试1:流转换法,报“Invalid Parameter”错误

private Texture2D baseToTex(string base64)
{
    Texture2D tex = new Texture2D(1, 1); //Texture setup

    string b64_string = base64;
    byte[] b64_bytes_webp = System.Convert.FromBase64String(b64_string);

    byte[] b64_bytes_png = RewriteBytesToPng(b64_bytes_webp);

    File.WriteAllBytes(@"C:\\Users\Limofeus\Desktop\img.png", b64_bytes_png);
    byte[] bytes = File.ReadAllBytes(@"C:\\Users\Limofeus\Desktop\img.png");

    tex.LoadImage(bytes); //Load and apply Texture from bytes
    tex.Apply();
    return tex;
}

private byte[] RewriteBytesToPng(byte[] data)
{
    using (var inStream = new MemoryStream(data))
    using (var outStream = new MemoryStream())
    {
        var imageStream = System.Drawing.Image.FromStream(inStream);
        imageStream.Save(outStream, System.Drawing.Imaging.ImageFormat.Png);
        return outStream.ToArray();
    }
}

失败尝试2:直接Bitmap转换,报类似错误

private Texture2D baseToTex(string base64)
{
    Texture2D tex = new Texture2D(1, 1); //Texture setup

    string b64_string = base64;
    byte[] b64_bytes_webp = System.Convert.FromBase64String(b64_string);

    var stream = new MemoryStream(b64_bytes_webp);
    Image img = new Bitmap(stream);
    img.Save(@"C:\\Users\Limofeus\Desktop\img.png", System.Drawing.Imaging.ImageFormat.Png);

    byte[] bytes = File.ReadAllBytes(@"C:\\Users\Limofeus\Desktop\img.png");

    tex.LoadImage(bytes); //Load and apply Texture from bytes
    tex.Apply();
    return tex;
}

失败尝试3:先存WebP再转换,报内存不足错误

private Texture2D baseToTex(string base64)
{
    Texture2D tex = new Texture2D(1, 1); //Texture setup

    string b64_string = base64;
    byte[] b64_bytes_webp = System.Convert.FromBase64String(b64_string);

    File.WriteAllBytes(@"C:\\Users\Limofeus\Desktop\img.webp", b64_bytes_webp);

    using (Image image = Image.FromFile(@"C:\\Users\Limofeus\Desktop\img.webp"))
    {
        Image img = new Bitmap(image);
        img.Save(@"C:\\Users\Limofeus\Desktop\img.png", System.Drawing.Imaging.ImageFormat.Png);
    }

    byte[] bytes = File.ReadAllBytes(@"C:\\Users\Limofeus\Desktop\img.png");

    tex.LoadImage(bytes); //Load and apply Texture from bytes
    tex.Apply();
    return tex;
}

请问是否可以通过.NET实现WebP转PNG?若可以,该如何实现?


解决方案

可以通过.NET实现WebP转PNG,你之前尝试失败的核心原因是**System.Drawing原生不支持WebP格式**。以下是两种可行的实现方式:

方式1:使用Google官方WebP库

Google提供了官方的WebP处理库,通过NuGet安装Google.WebP包后,可直接解码WebP并转换为PNG:

private Texture2D Base64WebpToTexture2D(string base64)
{
    byte[] webpData = Convert.FromBase64String(base64);
    byte[] pngData;

    // 内存中解码WebP为Bitmap
    using (var webpStream = new MemoryStream(webpData))
    using (var bitmap = Google.WebP.Decoder.Decode(webpStream))
    {
        // 将Bitmap转为PNG字节数组
        using (var pngStream = new MemoryStream())
        {
            bitmap.Save(pngStream, System.Drawing.Imaging.ImageFormat.Png);
            pngData = pngStream.ToArray();
        }
    }

    // 加载为Unity Texture2D
    Texture2D tex = new Texture2D(1, 1);
    tex.LoadImage(pngData);
    tex.Apply();
    return tex;
}

方式2:使用跨平台第三方库ImageSharp

ImageSharp是轻量跨平台的图像处理库,无需依赖GDI+,更适合Unity场景:

  1. 通过NuGet安装SixLabors.ImageSharp和SixLabors.ImageSharp.Formats.Webp包
  2. 使用以下代码转换:
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.Formats.Webp;
using SixLabors.ImageSharp.PixelFormats;

private Texture2D Base64WebpToTexture2D(string base64)
{
    byte[] webpData = Convert.FromBase64String(base64);
    byte[] pngData;

    using (var webpStream = new MemoryStream(webpData))
    using (Image<Rgba32> image = Image.Load<Rgba32>(webpStream, new WebpDecoder()))
    {
        using (var pngStream = new MemoryStream())
        {
            image.Save(pngStream, new PngEncoder());
            pngData = pngStream.ToArray();
        }
    }

    Texture2D tex = new Texture2D(1, 1);
    tex.LoadImage(pngData);
    tex.Apply();
    return tex;
}

注意事项

  • 无需写入本地文件,直接在内存中处理即可,减少IO开销和路径权限问题
  • Unity环境优先选择ImageSharp,避免System.Drawing带来的平台兼容性问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 14:35:20