且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在C#中重复图像

更新时间:2023-10-27 23:08:16

在C#中,您可以创建一个TextureBrush,它将在您使用的任何位置平铺图像,然后用它填充一个区域.像这样(一个填充整个图像的示例)...

In C#, you can create a TextureBrush that'll tile your image wherever you use it, and then fill an area with it. Something like this (an example that fills the whole image)...

// Use `using` blocks for GDI objects you create, so they'll be released
// quickly when you're done with them.
using (TextureBrush brush = new TextureBrush(yourImage, WrapMode.Tile))
using (Graphics g = Graphics.FromImage(destImage))
{
    // Do your painting in here
    g.FillRectangle(brush, 0, 0, destImage.Width, destImage.Height);
}

注意,如果要控制图像的平铺方式,则需要学习一些有关变换的知识.

Note, if you want some control over how the image is tiled, you're going to need to learn a bit about transforms.

我差点忘了(实际上我确实忘记了一点):您需要导入System.Drawing(对于GraphicsTextureBrush)和System.Drawing.Drawing2D(对于WrapMode)以获取代码.可以按原样工作.

I almost forgot (actually I did forget for a bit): You'll need to import System.Drawing (for Graphics and TextureBrush) and System.Drawing.Drawing2D (for WrapMode) in order for the code above to work as is.