繁体   English   中英

如何在图像上引入叠加层

[英]How can I introduce an overlay on an image

如何操作图像以添加半透明的1x1格式覆盖,就像在C#中的第二个图像一样?

在此输入图像描述在此输入图像描述

将原始图像加载到system.Drawing.Image中,然后从中创建图形对象。 加载要绘制的检查器图案的第二个图像,然后使用您创建的图形对象在原始图像上重复绘制检查器图像。

未经测试的例子

    Image Original;
    Image Overlay;

    Original = new Bitmap(100, 100, System.Drawing.Imaging.PixelFormat.Format32bppArgb); //Load your real image here.
    Overlay = new Bitmap(2, 2 ,System.Drawing.Imaging.PixelFormat.Format32bppArgb);//Load your 2x2 (or whatever size you want) overlay image here.

    Graphics gr = Graphics.FromImage(Original);
    for (int y = 0; y < Original.Height + Overlay.Height; y = y + Overlay.Height)
    {
        for (int x = 0; x < Original.Width + OverlayWidth; x = x + Overlay.Width)
        {
            gr.DrawImage(Overlay, x, y);
        }  
    }
    gr.Dispose();

代码执行后,Original现在将包含应用了覆盖的原始图像。

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

在此输入图像描述在此输入图像描述

// 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");
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM