gpt4 book ai didi

c# - 以编程方式用白色填充替换图像中的透明区域?

转载 作者:行者123 更新时间:2023-12-04 00:10:14 26 4
gpt4 key购买 nike

我有一个 PNG 图像,我正在通过 .NET 中的 System.Drawing API 对其进行操作。它有很大的透明区域,我想用白色填充替换透明区域——这样图像中就没有透明区域了。在图像编辑程序中很容易......但到目前为止,我在 C# 中没有成功。

谁能指点一下?

最佳答案

我不确定如何检测透明像素。我知道如果 Alpha 为 0,它是完全透明的,如果是 255,它是不透明的。我不确定您是否应该检查 Alpha == 0 或 Alpha != 255 ;如果您可以尝试并给我一个有用的反馈。

来自 MSDN

The alpha component specifies the transparency of the color: 0 is fully transparent, and 255 is fully opaque. Likewise, an A value of 255 represents an opaque color. An A value from 1 through 254 represents a semitransparent color. The color becomes more opaque as A approaches 255.

    void  Foo(Bitmap image)
{
for (int y = 0; y < image.Height; ++y)
{
for (int x = 0; x < image.Width; ++x)
{
// not very sure about the condition.
if (image.GetPixel(x, y).A != 255)
{
image.SetPixel(x,y,Color.White);
}
}
}

}

关于c# - 以编程方式用白色填充替换图像中的透明区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5425173/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com