【发布时间】:2014-01-22 22:14:22
【问题描述】:
试图找出在 C# 中以特定颜色的遮罩呈现图像的最优雅的方法(通过 System.Drawing 或可在桌面和 ASP.NET 应用程序中使用的等效方法)。
蒙版图像将包含应该“绘制”图像的绿色键。
(下面的预期结果图像并不完美,手套索...)
【问题讨论】:
标签: c# mask imaging system.drawing.imaging
试图找出在 C# 中以特定颜色的遮罩呈现图像的最优雅的方法(通过 System.Drawing 或可在桌面和 ASP.NET 应用程序中使用的等效方法)。
蒙版图像将包含应该“绘制”图像的绿色键。
(下面的预期结果图像并不完美,手套索...)
【问题讨论】:
标签: c# mask imaging system.drawing.imaging
为此有多种技术:
扫描像素数据并构建掩码图像(如 itsme86 和 Moby Disk 所建议的那样)
一种扫描变体,它从蒙版构建一个剪切区域并在绘图时使用该区域(请参阅 Bob Powell 的 this article)
在Graphics.DrawImage 调用中使用颜色键进行屏蔽。
我将专注于第三个选项。
假设您要从蒙版中消除的图像颜色是Color.Lime,我们可以使用ImageAttributes.SetColorKey 来阻止在调用Graphics.DrawImage 期间绘制任何该颜色,如下所示:
using (Image background = Bitmap.FromFile("tree.png"))
using (Image masksource = Bitmap.FromFile("mask.png"))
using (var imgattr = new ImageAttributes())
{
// set color key to Lime
imgattr.SetColorKey(Color.Lime, Color.Lime);
// Draw non-lime portions of mask onto original
using (var g = Graphics.FromImage(background))
{
g.DrawImage(
masksource,
new Rectangle(0, 0, masksource.Width, masksource.Height),
0, 0, masksource.Width, masksource.Height,
GraphicsUnit.Pixel, imgattr
);
}
// Do something with the composited image here...
background.Save("Composited.png");
}
结果:
如果您想将树的这些部分放入另一个图像中,您可以使用相同的技术(在Color.Fuchsia 上使用颜色键)。
【讨论】:
你想要这样的东西:
Bitmap original = new Bitmap(@"tree.jpg");
Bitmap mask = new Bitmap(@"mask.jpg");
int width = original.Width;
int height = original.Height;
// This is the color that will be replaced in the mask
Color key = Color.FromArgb(0,255,0);
// Processing one pixel at a time is slow, but easy to understand
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
// Is this pixel "green" ?
if (mask.GetPixel(x,y) == key)
{
// Copy the pixel color from the original
Color c = original.GetPixel(x,y);
// Into the mask
mask.SetPixel(x,y,c);
}
}
}
【讨论】:
您可能会读入掩码并将其转换为图像,当像素为绿色时,alpha 通道设置为 0,当像素为任何其他颜色时,alpha 通道设置为 0xFF。然后您可以在原始图像上绘制蒙版图像。
【讨论】: