簡體   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