Replacing transparent background with white color in PNG images

前端 未结 1 984
灰色年华
灰色年华 2020-12-03 22:57

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

相关标签:
1条回答
  • 2020-12-03 23:06

    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.

    0 讨论(0)
提交回复
热议问题