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.56k stars 543 forks source link

Overlay Text Color is not changing #2644

Open PKYADAV opened 1 year ago

PKYADAV commented 1 year ago

Description

I am trying to write overlay text on images using Skiasharp and it's working but I am not able to change the text color from white to any other colors.

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:

void OverlayImage(string? path)
        {
            var resizeFactor = 0.9f;
            var bitmap = SKBitmap.Decode(path);

            var toBitmap = new SKBitmap((int)Math.Round(bitmap.Width * resizeFactor), (int)Math.Round(bitmap.Height * resizeFactor), bitmap.ColorType, bitmap.AlphaType);

            var canvas = new SKCanvas(toBitmap);
            // Draw a bitmap rescaled
            canvas.SetMatrix(SKMatrix.CreateScale(resizeFactor, resizeFactor));
            canvas.DrawBitmap(bitmap, 0, 0);
            canvas.ResetMatrix();
            float textSize = 12;

            var font = SKTypeface.FromFamilyName("Arial");
            var brush = new SKPaint
            {
                Typeface = font,
                TextSize = textSize,
                IsAntialias = true,
                Color = SKColor.Parse("#39FF14")
            };
            string drawText = $"Test Text: ABCD132456";
            canvas.DrawText(drawText, 10, 15, brush);
            canvas.DrawText("www.nidoworld.com", 10, bitmap.Height - (bitmap.Height * 13 / 100), brush);

            canvas.Flush();

            var image = SKImage.FromBitmap(toBitmap);
            var data = image.Encode(SKEncodedImageFormat.Jpeg, 90);

            var stream = new MemoryStream();
            data.SaveTo(stream);

            if (_isImageSave == true)
            {
                string? filePath = "some path";
                using FileStream file = new(filePath, FileMode.Create, FileAccess.Write);
                stream.WriteTo(file);
            }

            var bytesString = Convert.ToBase64String(stream.ToArray());
            data.Dispose();
            image.Dispose();
            canvas.Dispose();
            brush.Dispose();
            font.Dispose();
            toBitmap.Dispose();
            bitmap.Dispose();
            stream.Dispose();
        }

Expected Behavior

Text Color must be in green.

Actual Behavior

Text color is always white

Version of SkiaSharp

2.88.3 (Current)

Last Known Good Version of SkiaSharp

2.88.2 (Previous)

IDE / Editor

Visual Studio (Windows)

Platform / Operating System

Linux

Platform / Operating System Version

Ubuntu 22.04 Ubuntu 20.04

Devices

No response

Relevant Screenshots

No response

Relevant Log Output

No response

Code of Conduct

PKYADAV commented 1 year ago

Any update or information on this