c# Replace transparent area from image with a color

210 views Asked by At

I receive an image from a Flutter mobile app like this (base64): https://next-at-ms.com/base64data.txt back to image is something like this: https://next-at-ms.com/imagedata.bmp

i need to include this image in a Fastreport.NET pdf report but it appear like a full dark image

as far as i understood it is an image/bmp type

Following other questions similar here, i try looping pixel by pixel, i removed alpha channel, i try a suggestion based on PdfSharp / Bitmapsources /BmpDitmapDecoder and combinations of them; i tried the accepted and even other answers with no luck.

I noticed that opening the file in windows explorer the image looks like full dark while in OSX preview I see the transparent image correctly same as in Chrome Browser in both operating systems

Any idea?

1

There are 1 answers

0
Andrii Khomiak On

You can set background color with SixLabors.ImageSharp

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
...

var b64Img = "your-base64-image-string";
var imageBytes = Convert.FromBase64String(b64Img);
using (var ms = new MemoryStream())
{
    using (var image = Image.Load(imageBytes))
    {
        image.Mutate(x => x.BackgroundColor(Color.White));
        image.SaveAsJpeg(ms);
    }
    var b64White = Convert.ToBase64String(ms.ToArray());
}