如何为PNG添加彩色背景或向图像添加颜色叠加层?

时间:2018-07-28 11:46:21

标签: c# image uwp png

为PNG图像提供彩色背景的合适蜡是什么?如下例所示:
具有不同级别的不透明度和透明背景的PNG图片: A PNG Image with different levels of opacity and transparent background

已应用背景颜色的PNG图像: A PNG image with a background color applied

问题是要使用指定的颜色从旧的图像中复制新的图像。

第二部分是如何向PNG图像添加颜色叠加层。也许用新颜色替换白色。

这是我的示例PNG: This is the example PNG I have

这是我要获取的示例PNG: And this is the example PNG I want to get

1 个答案:

答案 0 :(得分:0)

如果我错了,请纠正我..但是使用灰度图像代替带有Alpha通道的PNG可能是一种可能性

Bitmap colorBitmap(Color forGroundColor, Color backGroundColor, Bitmap bmpGrayScale)
{
  Size size = bmpGrayScale.Size;
  Bitmap applyForgroundColor= new Bitmap(size.Width, size.Height);
  Rectangle rect1 = new Rectangle(Point.Empty, bmpGrayScale.Size);
  using (Graphics G1 = Graphics.FromImage(applyForgroundColor) )
  {
    G1.Clear(forGroundColor);
    G1.DrawImageUnscaledAndClipped(applyForgroundColor, rect1);
  }
  for (int y = 0; y < size.Height; y++)
    for (int x = 0; x < size.Width; x++)
    {
      Color c1 = applyForgroundColor.GetPixel(x, y);
      Color c2 = bmpGrayScale.GetPixel(x, y);
      applyForgroundColor.SetPixel(x, y, Color.FromArgb((int)(255 * c2.GetBrightness()), c1 ) );
    }

  Bitmap applyBackgroundColor= new Bitmap(size.Width, size.Height);
  Rectangle rect2 = new Rectangle(Point.Empty, bmpGrayScale.Size);
  using (Graphics G2 = Graphics.FromImage(applyBackgroundColor) )
  {
    G2.Clear(backGroundColor);
    G2.DrawImageUnscaledAndClipped(applyForgroundColor, rect2);
  }
  return applyBackgroundColor;
}