将透明png颜色转换为单色

jes*_*ind 6 c# transparency bitmap

我正在使用Bitmap C#并想知道如何将颜色png图像转换为仅一种颜色.我希望图像中的所有可见颜色都变白.透明的部分应保持透明.我将以灰色背景显示这些.

Clo*_*ger 7

如果图像不使用alpha通道获得透明度,则以下内容将执行:

Bitmap image;

for (int x = 0; x < image.Width; x++)
{
    for (int y = 0; y < image.Height; y++)
    {
        if (image.GetPixel(x, y) != Color.Transparent)
        {
            image.SetPixel(x, y, Color.White);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


jes*_*ind 7

其他答案很有帮助,让我走了,非常感谢.我不能让他们工作,不知道为什么.但我也发现我想保留像素的原始alpha值,使边缘平滑.这就是我提出的.

for (int x = 0; x < bitmap.Width; x++)
{
    for (int y = 0; y < bitmap.Height; y++)
    {
        Color bitColor = bitmap.GetPixel(x, y);
        //Sets all the pixels to white but with the original alpha value
        bitmap.SetPixel(x, y, Color.FromArgb(bitColor.A, 255, 255, 255));
    }
}
Run Code Online (Sandbox Code Playgroud)

这是一个放大几次的结果的屏幕转储(原始在上面): 替代文字http://codeodyssey.se/upload/white-transparent.png