c# - 如何在图像上引入叠加层

标签 c# image image-processing

我如何操作图像以添加半透明的 1x1 选中叠加层,就像 C# 中的第二个图像一样?

enter image description here enter image description here

最佳答案

我能够修改我刚才发布的答案并在代码中创建叠加层。创建叠加图像后,我使用 TextureBrush 填充原始图像的区域。下面代码中的设置创建了以下图像;您可以根据需要更改尺寸和颜色。

enter image description here enter image description here

// set the light and dark overlay colors
Color c1 = Color.FromArgb(80, Color.Silver);
Color c2 = Color.FromArgb(80, Color.DarkGray);

// set up the tile size - this will be 8x8 pixels, with each light/dark square being 4x4 pixels
int length = 8;
int halfLength = length / 2;

using (Bitmap overlay = new Bitmap(length, length, PixelFormat.Format32bppArgb))
{
    // draw the overlay - this will be a 2 x 2 grid of squares,
    // alternating between colors c1 and c2
    for (int x = 0; x < length; x++)
    {
        for (int y = 0; y < length; y++)
        {
            if ((x < halfLength && y < halfLength) || (x >= halfLength && y >= halfLength)) 
                overlay.SetPixel(x, y, c1);
            else 
                overlay.SetPixel(x, y, c2);
        }
    }

    // open the source image
    using (Image image = Image.FromFile(@"C:\Users\Public\Pictures\Sample Pictures\homers_brain.jpg"))
    using (Graphics graphics = Graphics.FromImage(image))
    {
        // create a brush from the overlay image, draw over the source image and save to a new image
        using (Brush overlayBrush = new TextureBrush(overlay))
        {
            graphics.FillRectangle(overlayBrush, new Rectangle(new Point(0, 0), image.Size));
            image.Save(@"C:\Users\Public\Pictures\Sample Pictures\homers_brain_overlay.jpg");
        }
    }
}

关于c# - 如何在图像上引入叠加层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7898433/

相关文章:

c# - 通过字符串将方法名称分配给Func

c# - 当你有多个 visual studio 解决方案时如何使用重构工具?

javascript - 在网页中打印多个选定的图像

c# - ASP .NET Core IIS 托管用户身份名称为空且 IsAuthenticated=false

c# - 在 C# 中打开 Word 文档时显示修订气球

javascript - JavaScript 中的图像失真算法

html - 有一个较小的图像然后 div 拉伸(stretch)以填充 div 的区域而不会破坏图像比例

image-processing - 区域增长与聚类

ios - CIColorMatrix Filter 结果很奇怪

python-2.7 - 如何使用 python 检测颜色组合?