我有一个来自Android中的 DrawingView
的PNG图像,正在发送到WCF服务。该图像以32位格式发送,并具有透明背景。我想用白色替换透明颜色(换句话说)背景。到目前为止,我的代码看起来像这样:
// 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);
除了图片有透明背景外,以上方法都正常运作。我注意到在Paint.NET中,你可以将PNG格式设置为8位,这会将背景设为白色。然而,当我尝试使用以下代码时:
System.Drawing.Imaging.PixelFormat.Format8bppIndexed
我得到的只是一张完全黑色的图片。
问题:如何将png文件中的透明背景替换为白色?
附注:该图片为灰度图像。