Hi folks., i have an image with certain pattern.. i need to repeat it in another image using GDi? is there any method to do in GDI?
A:
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);
}
}
}
Adam Robinson
2010-04-20 13:09:55
@sam: You pass it the rectangle on your `Graphics` object that you want to fill with the image.
Adam Robinson
2010-04-21 14:47:14
+4
A:
In C#, you can create a TextureBrush that'll tile your image wherever you use it. Something like
TextureBrush brush = new TextureBrush(yourImage);
brush.WrapMode = WrapMode.Tile;
Then use the brush to fill an area where you want the image to be tiled. (This example fills the whole image.)
Graphics g = Graphics.FromImage(someOtherImage);
g.FillRectangle(brush, 0, 0, someOtherImage.Width, someOtherImage.Height);
Note, if you want some control over how the image is tiled, you're gonna need to learn a bit about transforms.
Almost forgot (actually i did forget for a bit)...you'll need to make sure System.Drawing and System.Drawing.Drawing2D are imported in order for the code above to work.
cHao
2010-04-20 13:12:23