Replacing transparent background with white color in PNG images

Dawid O

I have a PNG image being sent from a DrawingView in Android to a WCF service. The image is sent as a 32-bit and it has transparent background. I want to replace the transparent colour (for lack of a better word) background with white. So far my code looks like this:

// Converting image to Bitmap object
Bitmap i = new Bitmap(new MemoryStream(Convert.FromBase64String(image)));
// The image that is send from the tablet is 1280x692
// So we need to crop it
Rectangle cropRect = new Rectangle(640, 0, 640, 692);
//HERE
Bitmap target = i.Clone(cropRect, i.PixelFormat);
target.Save(string.Format("c:\\images\\{0}.png", randomFileName()),
System.Drawing.Imaging.ImageFormat.Png);

The above works fine, except the images have transparent background. I noticed that in Paint.NET you can simply set the PNG format to 8-bit and it sets the background to white. However, when I tried using:

System.Drawing.Imaging.PixelFormat.Format8bppIndexed

all I got was a completely black picture.

Q: How to replace the transparent background with white in a png?

PS. The image is in Gray Scale.

TaW

This will draw onto a given color:

Bitmap Transparent2Color(Bitmap bmp1, Color target)
{
    Bitmap bmp2 = new Bitmap(bmp1.Width, bmp1.Height);
    Rectangle rect = new Rectangle(Point.Empty, bmp1.Size);
    using (Graphics G = Graphics.FromImage(bmp2) )
    {
        G.Clear(target);
        G.DrawImageUnscaledAndClipped(bmp1, rect);
    }
    return bmp2;
}

This makes use of the G.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;, which is the default. It blends the drawn image with the background according to the alpha channel of the drawn image.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Open CV - iOS PNG Overlaying images with transparent background shows white background

PNG with white background renders as transparent on mobile Chrome

Saving transparent PNG as JPEG on white background

Create transparent background for white png image Python

Replace transparency in PNG images with white background

Flutter not rendering color for transparent background png icon

UIView background color and transparent png on top

Find out PNG transparent background color on Linux

Background color over transparent PNG image in CSS

ffmpeg hstack png and replace transparent color with white color

php merge transparent png image to jpeg preventing the white background

Read transparent images as white background scikit-image python

Background-color white gets transparent with opacity set to 1

What is the default background color for HTML elements? White or Transparent?

How to change the background color of the heading from transparent to paint white?

Transparent background turns white

png images with transparent background don't work in pygame 2.0.0

Changing a transparent images background color after clicking on a seperate div

How set white color background when concatenating images via python?

Issue with transparent images with a transparent background

certain images only are appearing with a white background behind them, even though the images are transparent

Is it possible to make the "Presentation Hosting Controller" background color transparent instead of white color in SwiftUI?

Custom dialog with white transparent background

Transparent border with background color

Export plot in .png with transparent background

Background color is leaking into transparent color

UWP Transparent png color overlay

How to make the white of a PNG transparent in Java?

OpenCV - Turn Transparent Part of PNG white