在C#中使用TextureBrush从不同高度开始平铺图像时出现问题
我正在尝试使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了确保矩形的起点从图像的起点开始,我们使用了变换,如下面的代码所示。
我发现这个链接很有帮助。这详细解释了画笔和变换。
To ensure that start of the rectangle starts with start of the image we have use transforms as shown in below code.
I found this link helpful. This explains about brushes and transforms in detail.
我在 Windows 窗体应用程序中尝试了此操作,它按预期工作,当 y == 14 时在 (0, 14) 处绘制。是否有可能将
y
设置为 16、32 等您分配它的时间和创建矩形的时间?这是我用来测试的代码:
和结果:
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:
and the result:
使用 TransalteTransform() 设置起点,如果不这样做,平铺会有一些偏移,为了纠正这一点,如下所示:
using TransalteTransform() to set up start point,if u do not do this, tiling would have some offset, to corect this, like below: