I'm trying to write a routine to tile a given image in a given rectangle:

public static void DrawTiledImage(Graphics g, Image image, Rectangle r)
{
ImageAttributes ia = new ImageAttributes();
ia.SetWrapMode(WrapMode.Tile);
g.DrawImage(image, r, 0, 0, image.Width, image.Height,
GraphicsUnit.Pixel, ia);
}

Problem is this seems to be stretching the image not tiling it. What am I
doing wrong ?

Re: Tiling an image within a rectangle (GDI+) by Michael

Michael
Wed Jan 04 11:55:12 CST 2006

Just make your source rectangle larger than your image.

For example:
public static void DrawTiledImage(Graphics g, Image image, Rectangle r)
{
ImageAttributes ia = new ImageAttributes();
ia.SetWrapMode(WrapMode.Tile);
g.DrawImage(image, r, 0, 0, 4*image.Width, 4*image.Height,
GraphicsUnit.Pixel, ia);
}



"JezB" <jezbroadsword@blueyonder.co.uk> wrote in message
news:Ogt8LFVEGHA.1424@TK2MSFTNGP12.phx.gbl...
> I'm trying to write a routine to tile a given image in a given rectangle:
>
> public static void DrawTiledImage(Graphics g, Image image, Rectangle r)
> {
> ImageAttributes ia = new ImageAttributes();
> ia.SetWrapMode(WrapMode.Tile);
> g.DrawImage(image, r, 0, 0, image.Width, image.Height,
> GraphicsUnit.Pixel, ia);
> }
>
> Problem is this seems to be stretching the image not tiling it. What am I
> doing wrong ?
>
>



Re: Tiling an image within a rectangle (GDI+) by Herfried

Herfried
Wed Jan 04 11:58:11 CST 2006

"JezB" <jezbroadsword@blueyonder.co.uk> schrieb:
> I'm trying to write a routine to tile a given image in a given rectangle:

The snippet below should point you into the right direction:

\\\
Private m_Brush As TextureBrush

Private Sub Form1_Load(...) Handles MyBase.Load
m_Brush = New TextureBrush(New Bitmap("C:\WINDOWS\Angler.bmp"))
End Sub

Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs)
MyBase.OnPaintBackground(e)
e.Graphics.FillRectangle(m_Brush, Me.ClientRectangle)
End Sub
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Re: Tiling an image within a rectangle (GDI+) by JezB

JezB
Tue Jan 17 10:55:17 CST 2006

This does not quite work, in that the image does not always start at the top
left pixel within the target rectangle. While this is not always important,
it is in my case.

"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
news:em13phVEGHA.344@TK2MSFTNGP11.phx.gbl...
> "JezB" <jezbroadsword@blueyonder.co.uk> schrieb:
>> I'm trying to write a routine to tile a given image in a given rectangle:
>
> The snippet below should point you into the right direction:
>
> \\\
> Private m_Brush As TextureBrush
>
> Private Sub Form1_Load(...) Handles MyBase.Load
> m_Brush = New TextureBrush(New Bitmap("C:\WINDOWS\Angler.bmp"))
> End Sub
>
> Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs)
> MyBase.OnPaintBackground(e)
> e.Graphics.FillRectangle(m_Brush, Me.ClientRectangle)
> End Sub
> ///
>
> --
> M S Herfried K. Wagner
> M V P <URL:http://dotnet.mvps.org/>
> V B <URL:http://classicvb.org/petition/>



Re: Tiling an image within a rectangle (GDI+) by JezB

JezB
Tue Jan 17 10:57:29 CST 2006

While I do not quite understand why this should work, it does, up to a
point. Actually I find that since my image is 1 pixel wide and 21 high,
inflating just the source width and not the height has the desired effect. I
was hoping to write a routine that is a bit more generic for tiling any
image within any rectangle though.

"Michael Phillips, Jr." <mphillips53@nospam.jun0.c0m> wrote in message
news:Oo2g$fVEGHA.1384@TK2MSFTNGP11.phx.gbl...
> Just make your source rectangle larger than your image.
>
> For example:
> public static void DrawTiledImage(Graphics g, Image image, Rectangle r)
> {
> ImageAttributes ia = new ImageAttributes();
> ia.SetWrapMode(WrapMode.Tile);
> g.DrawImage(image, r, 0, 0, 4*image.Width, 4*image.Height,
> GraphicsUnit.Pixel, ia);
> }
>
>
>
> "JezB" <jezbroadsword@blueyonder.co.uk> wrote in message
> news:Ogt8LFVEGHA.1424@TK2MSFTNGP12.phx.gbl...
>> I'm trying to write a routine to tile a given image in a given rectangle:
>>
>> public static void DrawTiledImage(Graphics g, Image image, Rectangle r)
>> {
>> ImageAttributes ia = new ImageAttributes();
>> ia.SetWrapMode(WrapMode.Tile);
>> g.DrawImage(image, r, 0, 0, image.Width, image.Height,
>> GraphicsUnit.Pixel, ia);
>> }
>>
>> Problem is this seems to be stretching the image not tiling it. What am I
>> doing wrong ?
>>
>>
>
>