views:

50

answers:

2

Hello.

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!

A: 

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
Thanks for your answer but this can be so slow.
VansFannel
Wow, that would be unbelievably slow.
ctacke
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
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
+1  A: 

Use Grapics.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 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).

ctacke