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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…