I'm developing a Windows Mobile application with C# and .NET Compact Framework.
I want to fill a Bitmap with an image t开发者_开发百科hat 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!
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).
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));
}
}
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
精彩评论