C# 如何在C语言中重复图像#

C# 如何在C语言中重复图像#,c#,image,image-manipulation,tile,C#,Image,Image Manipulation,Tile,我有一个特定图案的图像。如何使用GDI在另一个图像中重复它? 在GDI中有什么方法可以做到这一点吗?没有将特定图像绘制为“图案”(重复绘制)的功能,但它应该非常简单: public static void FillPattern(Graphics g, Image image, Rectangle rect) { Rectangle imageRect; Rectangle drawRect; for (int x = rect.X; x < rect.Right

我有一个特定图案的图像。如何使用GDI在另一个图像中重复它?

在GDI中有什么方法可以做到这一点吗?

没有将特定图像绘制为“图案”(重复绘制)的功能,但它应该非常简单:

public static void FillPattern(Graphics g, Image image, Rectangle rect)
{
    Rectangle imageRect;
    Rectangle drawRect;

    for (int x = rect.X; x < rect.Right; x += image.Width)
    {
        for (int y = rect.Y; y < rect.Bottom; y += image.Height)
        {
            drawRect = new Rectangle(x, y, Math.Min(image.Width, rect.Right - x),
                           Math.Min(image.Height, rect.Bottom - y));
            imageRect = new Rectangle(0, 0, drawRect.Width, drawRect.Height);

            g.DrawImage(image, drawRect, imageRect, GraphicsUnit.Pixel);
        }
    }
}
publicstaticvoidfillpattern(图形g、图像图像、矩形矩形矩形)
{
矩形imageRect;
矩形drawRect;
for(intx=rect.x;x
在C#中,您可以创建一个TextureBrush,它将在您使用图像的任何位置平铺图像,然后用它填充一个区域。类似这样的东西(一个充满整个图像的示例)

注意,如果您想控制图像的平铺方式,您需要了解一些有关变换的知识


我差点忘了(实际上我确实忘了一点):你需要导入
System.Drawing
(对于
Graphics
TextureBrush
)和
System.Drawing.Drawing2D
(对于
WrapMode
)才能让上面的代码正常工作。

什么样的模式?你想复制像素吗?@sam:你把你想用图像填充的
图形
对象上的矩形传递给它。
// 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);
}