WPF TileBrush 和 DrawingContext.DrawRectangle ——相对于矩形的左上角?
使用 DrawingContext 时。DrawRectangle 带有 TileBrush,我注意到左上角该矩形不是底层图像的左上角。 DrawingBrush 和ImageBrush,源为 绘图图像。有没有办法强制 DrawRectangle
始终将底层画笔对齐到左上角?这是一张图片:
两个矩形使用相同的画笔,但位于画布上的不同点屏幕。正如您所看到的,画笔的原点不同(看起来画笔在更大的区域上是连续的)。这是一个最小的重现:
private static readonly Brush _brush;
static CustomControl()
{
Uri uri = new Uri(@"pack://application:,,,/WpfApplication1;component/image.png", UriKind.Absolute);
BitmapImage img = new BitmapImage(uri);
Rect rect = new Rect(0, 0, img.Width, img.Height);
ImageDrawing drawing = new ImageDrawing(img, rect);
_brush = new DrawingBrush
{
Drawing = drawing,
Viewport = rect,
ViewportUnits = BrushMappingMode.Absolute,
TileMode = TileMode.Tile
};
_brush.Freeze();
}
protected override void OnRender(DrawingContext dc)
{
dc.DrawRectangle(_brush, null, new Rect(70, 70, 100, 150));
dc.DrawRectangle(_brush, null, new Rect(200, 200, 80, 120));
}
* 在这种情况下,ImageBrush 会给出正确的结果,但在我的程序中,我正在使用自定义 GeometryDrawing。
我尝试过使用钢笔画笔、更改 TileMode、将拉伸设置为 Uniform、更改 AlignmentX 和 AlignmentY... 似乎没有任何效果。画笔的原点永远不会设置为矩形的原点。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用的解决方案是使用 翻译转换。因此,
您可以使用:
这并不理想,因为每次绘制新矩形时都必须分配一个新的 TranslateTransform(以及另一个底层 DUCE 资源),但它执行在我的测试中足够好。
重要提示:在重新渲染时(例如滚动时),您可能仍会遇到一些奇怪(但微妙)的扭曲。这些可能是由子像素渲染系统引起的。您可以使用 GuidelineSet 或只需将起点的 X 和 Y 舍入到最接近的整数值即可。
The solution I used was to render it at (0,0) with a TranslateTransform. So instead of...
You can use:
This isn't ideal, since you have to allocate a new
TranslateTransform
(and therefore another underlying DUCE resource) every time you draw a new rectangle, but it performs well enough in my tests.IMPORTANT NOTE: You may still encounter some strange (but subtle) distortions on re-rendering, such as when scrolling. These might be caused by the subpixel rendering system. You can either play around with GuidelineSets or just round the start point's X and Y to the nearest integer value.
根据我的经验,平铺画笔非常依赖于
Viewbox
、ViewboxUnits
、Viewport
和ViewportUnits
属性之间的关系刷子。单位的两个属性都应设置为
Absolute
;Viewport
和Viewbox
应该具有相同的大小(99% 的情况)。详细地说,这不是我读过的“官方”内容,它只是来自(大量)观察:
Viewbox
是渲染绘图时考虑的大小。如果未设置,则路径的Data
将确定Viewbox
大小。另一方面,Viewport
是平铺画笔时考虑的大小。将
Viewbox
设置为小于Viewport
的尺寸会在画笔的每个“图块”之间引入间距。将Viewbox
设置为大于Viewport
将导致“图块”重叠(从逻辑上讲,在屏幕上“图块”似乎被剪切)。From my experience, tiling brushes is very dependent on the relation between
Viewbox
,ViewboxUnits
,Viewport
andViewportUnits
properties of the brush.Both properties for the units should be set to
Absolute
; theViewport
andViewbox
should be the same size (99% of the cases).To elaborate, this is not something "official" that I've read, it's only from (a lot of) observation: the
Viewbox
is the size considered for rendering the drawing. If it's not set, theData
of the path will determine theViewbox
size. On the other hand,Viewport
is the size considered while tiling the brush.Setting the
Viewbox
to smaller size thanViewport
will introduce spacing between each of the 'tiles' of the brush. Setting theViewbox
larger thanViewport
will cause the 'tiles' to overlap (logically, on the screen the 'tile' will seem to be cut).