c# - 如何向 PNG 添加彩色背景或向图像添加颜色叠加?
问题描述
给 PNG 图像提供彩色背景的正确蜡是什么?如下例所示:
具有不同级别不透明度和透明背景的 PNG 图像:
问题是用指定颜色从旧图像复制新图像。
第二部分是如何为 PNG 图像添加颜色叠加层。也许用新颜色代替白色。
解决方案
如果我错了,请纠正我..但这可能是使用灰度图像而不是带有 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;
}