How to repeat an image in C#

后端 未结 2 1072
清歌不尽
清歌不尽 2021-01-02 01:38

I have an image with a certain pattern. How do I repeat it in another image using GDI?
Is there any method to do it in GDI?

2条回答
  •  我在风中等你
    2021-01-02 02:12

    There's no function to paint a particular image as a "pattern" (painting it repeatedly), but it should be pretty simple to do:

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

提交回复
热议问题