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

SKImage加载黄色PNG图像却渲染为蓝色的技术求助

问题

绘制PNG图像时,图像呈现蓝色色调。

代码

已更新,包含canvas的创建方式

SKImageInfo info = new SKImageInfo(2000, 2000);
SKBitmap bitmap = new SKBitmap(info);
Texture texture = new Texture((uint)bitmap.Width, (uint)bitmap.Height);
Sprite sprite = new Sprite(texture);
var surface = SKSurface.Create(bitmap.Info, bitmap.GetPixels(), bitmap.RowBytes);
var canvas = surface.Canvas;

var X = 0;
var Y = 0;
var paint = new SKPaint {
    IsAntialias = true,
    Color = SKColors.White,
    BlendMode = SKBlendMode.SrcOver,
    IsDither = true,
    ColorFilter = SKColorFilter.CreateBlendMode(SKColors.White, SKBlendMode.Modulate)
};
var cursorImage = SKImage.FromEncodedData("my_yellow_file.png");
var cursorPositionSrc = new SKRect(0, 0, cursorImage.Width, cursorImage.Height);
var cursorPositionDest = new SKRect(
    X,
    Y - (cursorImage.Height / 2),
    X + cursorImage.Width,
    Y + (cursorImage.Height / 2)
);
// canvas is a large surface to draw on
canvas.DrawImage(this.cursorImage, cursorPositionSrc, this.cursorPositionDest, this.cursorPaint);
图像
  • 原始图像:原本为黄色的PNG文件
  • 图像元数据:无外链展示
渲染/绘制结果
  • 结果图像:原本黄色的图像渲染后呈现明显蓝色色调
设备规格
  • 操作系统:Win11
解决方案

针对图像偏蓝的问题,可尝试以下修复步骤:

1. 移除冗余的颜色滤镜

当前SKPaint中的ColorFilter设置为白色与Modulate模式,虽理论上不改变颜色,但可能因图像Alpha通道或颜色空间差异引发异常。直接移除该滤镜:

var paint = new SKPaint {
    IsAntialias = true,
    Color = SKColors.White,
    BlendMode = SKBlendMode.SrcOver,
    IsDither = true
};

2. 显式指定图像信息的颜色格式

默认SKImageInfo的颜色类型可能与图像不匹配,显式设置为RGBA8888预乘Alpha格式,确保颜色通道解析正确:

SKImageInfo info = new SKImageInfo(2000, 2000, SKColorType.Rgba8888, SKAlphaType.Premul);

3. 修正Paint引用错误

代码中定义了paint变量,但绘制时使用this.cursorPaint,若cursorPaint存在错误配置会导致色调异常,需确保引用正确:

canvas.DrawImage(cursorImage, cursorPositionSrc, cursorPositionDest, paint);

4. 转换图像颜色格式(可选)

若原始图像颜色通道顺序为BGR,加载后转换为SkiaSharp默认的RGBA格式:

using var cursorImage = SKImage.FromEncodedData("my_yellow_file.png");
using var convertedImage = cursorImage.ToImage(SKColorType.Rgba8888, SKAlphaType.Premul);
// 使用convertedImage进行绘制
canvas.DrawImage(convertedImage, cursorPositionSrc, cursorPositionDest, paint);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 23:38:29