mono / SkiaSharp

SkiaSharp is a cross-platform 2D graphics API for .NET platforms based on Google's Skia Graphics Library. It provides a comprehensive 2D API that can be used across mobile, server and desktop models to render images.
MIT License
4.14k stars 522 forks source link

[BUG] <Image background color is different for Jpeg compared to PNG in Xamarin android> #2798

Open Yaavannvignesh opened 2 months ago

Yaavannvignesh commented 2 months ago

Description

We used SkiaSharp in the Xamarin Android platform to create a bitmap and applied a red background color to specific rectangles. We saved the bitmap as both PNG and JPEG image formats. However, we noticed that the JPEG image displays a different background color than the PNG image. This issue only occurs in Xamarin Android, as the background color displays correctly in both PNG and JPEG formats on Windows. Please check and update details

Code

The best way to share code for larger projects is a link to a GitHub repository: https://github.com/user/repo/tree/bug-123

But, you can also share a short block of code here:

using (var bitmap = new SKBitmap(100, 100))
{
    // Create SKCanvas with the bitmap
    using (var canvas = new SKCanvas(bitmap))
    {
        // Define rectangle parameters
        SKRect rectBounds = new SKRect(0, 0, 100, 100);
        SKPaint paint = new SKPaint
        {
            Color = new SKColor(255, 0, 0, 255)
        };

        // Draw the rectangle on the canvas
        canvas.DrawRect(rectBounds, paint);
    }

    // Create a MemoryStream to hold the image data
    var imageStream = new MemoryStream();
    bitmap.Encode(SKEncodedImageFormat.Jpeg, 100).SaveTo(imageStream);
    imageStream.Position = 0;

    return imageStream;
}

You can also share some XAML:

<!-- xaml code here -->

Expected Behavior

The image's background color should match the PNG.

Actual Behavior

The Background color should match the PNG image.

Version of SkiaSharp

2.88.6

Last Known Good Version of SkiaSharp

2.88.6

IDE / Editor

Visual Studio (Windows)

Platform / Operating System

Android

Platform / Operating System Version

Android

Devices

Oppo

Relevant Screenshots

https://github.com/mono/SkiaSharp/assets/131945130/dcfff30a-013a-444d-bd05-22e098091a08

Relevant Log Output

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAABHNCSVQICAgIfAhkiAAAAQBJREFUeJzt0UENACAQwLAD/57hjQL2aBUs2TozZ8jYvwN4GRJjSIwhMYbEGBJjSIwhMYbEGBJjSIwhMYbEGBJjSIwhMYbEGBJjSIwhMYbEGBJjSIwhMYbEGBJjSIwhMYbEGBJjSIwhMYbEGBJjSIwhMYbEGBJjSIwhMYbEGBJjSIwhMYbEGBJjSIwhMYbEGBJjSIwhMYbEGBJjSIwhMYbEGBJjSIwhMYbEGBJjSIwhMYbEGBJjSIwhMYbEGBJjSIwhMYbEGBJjSIwhMYbEGBJjSIwhMYbEGBJjSIwhMYbEGBJjSIwhMYbEGBJjSIwhMYbEGBJjSIwhMYbEGBJjSMwF7EwCxk+5tz8AAAAASUVORK5CYII=">

Code of Conduct