在C#中使用TextureBrush从不同高度开始平铺图像时出现问题

发布于 2024-12-03 09:12:57 字数 495 浏览 5 评论 0原文

我正在尝试使用TextureBrush 将图像(16x16)平铺在尺寸为width=1000、height=16 的矩形区域上以获得类似UI 的条带。

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

当我使用 x=0 进行绘制时,y=0 平铺会按预期从 (0,0) 开始进行。

当我使用 x=0, y=50 进行绘制时,平铺从 (0,50) 开始,但绘制矩形并不从图像的开头开始。它从图像的裁剪部分开始,然后重复。

怎么解决这个问题呢?

PS:我不想在 DrawImage 上手动循环重复平铺它。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谈场末日恋爱 2024-12-10 09:12:57

为了确保矩形的起点从图像的起点开始,我们使用了变换,如下面的代码所示。

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);
}

我发现这个链接很有帮助。这详细解释了画笔和变换。

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.

北陌 2024-12-10 09:12:57

我在 Windows 窗体应用程序中尝试了此操作,它按预期工作,当 y == 14 时在 (0, 14) 处绘制。是否有可能将 y 设置为 16、32 等您分配它的时间和创建矩形的时间?

这是我用来测试的代码:

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);
}

和结果:

在此处输入图像描述

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

毁梦 2024-12-10 09:12:57

使用 TransalteTransform() 设置起点,如果不这样做,平铺会有一些偏移,为了纠正这一点,如下所示:

brush.TranslateTransform(0,50);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文