Problem in tiling image starting at different height using TextureBrush in C#
Asked Answered
P

3

4

I am trying to tile an image(16x16) over a Rectangle area of dimensions width=1000, height=16 using TextureBrush to get a strip like UI.

 Rectangle myIconDrawingRectangle = new Rectangle(x, y, 1000, 16);
 using (TextureBrush brush = new TextureBrush(myIcon, WrapMode.Tile))
 {
    e.Graphics.FillRectangle(brush, myIconDrawingRectangle );
 }

When I draw with x=0, y=0 tiling happens as expected starting from (0,0).

When I draw with x=0, y=50 tiling starts at (0,50) but the the painting rectangle does not start with the start of the image. It starts with cropped portion of the image and then repeats.

How to solve this?

P.S: I do not want to tile it manually looping repeatedly over DrawImage.

Penetrating answered 7/9, 2011 at 12:26 Comment(0)
P
11

To ensure that start of the rectangle starts with start of the image we have use transforms as shown in below code.

Rectangle myIconDrawingRectangle = new Rectangle(x, y, 1000, 16);
using (TextureBrush brush = new TextureBrush(myIcon, WrapMode.Tile))
{
    brush.TranslateTransform(x,y);
    e.Graphics.FillRectangle(brush, myIconDrawingRectangle);
}

I found this link helpful. This explains about brushes and transforms in detail.

Penetrating answered 8/9, 2011 at 15:25 Comment(1)
Amazing, I struggled on this my entire Friday night. Monday morning I read this and it solved my problem. Thanks!Gallbladder
G
3

I tried this in a Windows Forms application, and it works as expected, drawing at (0, 14) when y == 14. Is it possible y is being set to 16, 32, etc. between the time you assign it and the time the rectangle is created?

Here is the code I used to test:

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);

    Image myIcon = Image.FromFile(@"C:\Users\me\Pictures\test.jpg");

    int x = 0;
    int y = 14;

    Rectangle myIconDrawingRectangle = new Rectangle(x, y, 1000, 16);
    using (TextureBrush brush = new TextureBrush(myIcon, WrapMode.Tile))
    {
        e.Graphics.FillRectangle(brush, myIconDrawingRectangle);
    }

    e.Graphics.DrawLine(Pens.Black, 0, 16, 1000, 16);
}

and the result:

enter image description here

Googins answered 7/9, 2011 at 13:50 Comment(4)
First of all, thanks for your reply. I need to modify the question now as it is wrong. I found out that now. As strange as it may have seem it appeared correct at that time. I have now found the actual issue and it's solution also. I'll explain in next comment.Penetrating
Thing is that image pixel pattern that I use is dark at top and gradually shades out at bottom, the color at the bottom resembling that of form background. The real issue is that when drawn at a height, it is not guaranteed that the painted rectangle area will start with the beginning of the image. Therefore, due to the nature of my image it gave an illusion that it was drawing at a different height. But actually it was drawing at correct height, just that the start of the rectangle did not start with start of the image. This is the problem to be addressed.I'll update the question accordingly.Penetrating
A vote up for verifying and confirming the correct behavior. That led me to look into the problem from a different angle.Penetrating
@Real Red, glad I was able to help out in some way :)Googins
A
1

using TransalteTransform() to set up start point,if u do not do this, tiling would have some offset, to corect this, like below:

brush.TranslateTransform(0,50);
Anticlinal answered 15/8, 2012 at 8:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.