2

I'm developing a Windows Mobile application with C# and .NET Compact Framework.

I want to fill a Bitmap with an image that it's smaller. To fill this new Bitmap I want to repeat the image horizontally and vertically until the bitmap it is completely fill.

How can I do that?

Thank you!

VansFannel
  • 45,055
  • 107
  • 359
  • 626

3 Answers3

1

Use Graphics.FromImage on your target to get a Graphics object, then use the DrawImage method on that resulting Graphics object to paint in your tile. Repeat for each rows and columns as necessary based on the size of the tile and the destination bitmap (i.e. offset x, y by the size of the tile and repeat).

RBK
  • 558
  • 5
  • 16
ctacke
  • 66,480
  • 18
  • 94
  • 155
0

Try this:

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));
    }
}
Daniel Pryden
  • 59,486
  • 16
  • 97
  • 135
  • Thanks for your answer but this can be so slow. – VansFannel Nov 16 '09 at 18:52
  • Wow, that would be unbelievably slow. – ctacke Nov 16 '09 at 20:24
  • Would it really be that slow? I'm honestly curious, I haven't worked with the `Bitmap` class much, but I've done plenty of this kind of thing with lower-level bitmap implementations, where this would be pretty blazing fast. Obviously, the `SetPixel` and `GetPixel` methods are suboptimal, but I would hope that the JIT could inline them and get decent performance. – Daniel Pryden Nov 16 '09 at 23:35
  • And, for the record, the question didn't ask for a *fast* way to do this, just one that works. And I'm pretty certain this approach would work. – Daniel Pryden Nov 16 '09 at 23:37
0

A TextureBrush can easily repeat an image across your entire surface. This is much easier than manually tiling an image across rows/columns.

Simply create the TextureBrush then use it to fill a rectangle. It'll automatically tile the image to fill the rectangle.

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

The above code is from a similar answer: https://stackoverflow.com/a/2675327/1145177

Community
  • 1
  • 1
Doug S
  • 10,146
  • 3
  • 40
  • 45