Search code examples
c#.netgraphicsgame-developmentskiasharp

SKImage loading image but drawing it as blue, when it has yellow color


Issue

When drawing a PNG image, it is drawn with a blue tint.

Code

updated to include how the canvas is created

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);

Image

Image Metadata Details

enter image description here

Rendering / Drawing the image

enter image description here

Machine Specs

OS: Win11


Solution

  • Found the issue, it was with the color space of the canvas

    SKImageInfo info = new SKImageInfo(2000, 2000);
    

    should be

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