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

前端 未结 3 1990
野的像风
野的像风 2021-01-20 21:28

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 myIconD         


        
3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-20 22:14

    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

提交回复
热议问题