簡體   English   中英

建立位圖以較小的圖像填充

[英]Build a Bitmap filling it with a smaller image

我正在使用C#和.NET Compact Framework開發Windows Mobile應用程序。

我想用較小的圖像填充位圖。 要填充此新位圖,我想水平和垂直重復圖像,直到位圖完全填充為止。

我怎樣才能做到這一點?

謝謝!

在目標上使用Graphics.FromImage來獲取Graphics對象,然后在所得的Graphics對象上使用DrawImage方法在圖塊中進行繪制。 根據圖塊的大小和目標位圖的需要,對每行和每列重復一次(即,將x,y偏移圖塊的大小並重復)。

嘗試這個:

for(int y = 0; y < outputBitmap.Height; y++) {
    for(int x = 0; x < outputBitmap.Width; x++) {
        int ix = x % inputBitmap.Width;
        int iy = y % inputBitmap.Height;
        outputBitmap.SetPixel(x, y, inputBitmap.GetPixel(ix, iy));
    }
}

TextureBrush可以輕松地在整個表面上重復圖像。 這比手動在行/列中平鋪圖像要容易得多。

只需創建TextureBrush然后使用它來填充矩形。 它將自動平鋪圖像以填充矩形。

using (TextureBrush brush = new TextureBrush(yourImage, WrapMode.Tile))
{
    using (Graphics g = Graphics.FromImage(destImage))
    {
        g.FillRectangle(brush, 0, 0, destImage.Width, destImage.Height);
    }
}

上面的代碼來自類似的答案: https : //stackoverflow.com/a/2675327/1145177

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM