WPF TileBrush 和 DrawingContext.DrawRectangle ——相对于矩形的左上角?

发布于 2024-11-29 14:41:15 字数 2150 浏览 2 评论 0 原文

使用 DrawingContext 时。DrawRectangle 带有 TileBrush,我注意到左上角该矩形不是底层图像的左上角。 DrawingBrushImageBrush,源为 绘图图像。有没有办法强制 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... 似乎没有任何效果。画笔的原点永远不会设置为矩形的原点。

When using DrawingContext.DrawRectangle with a TileBrush, I noticed that the top-left of the rectangle is not the top-left of the underlying image. This occurs with both a DrawingBrush and an ImageBrush with the source as a DrawingImage. Is there a way to force DrawRectangle to always snap the underlying brush to the top-left corner? Here's a picture:

An image of the brush alignment issues

The two rectangles are using the same brush but are at different points on the screen. As you can see, the brush's origin is different (it seems the brush is continuous over a much larger area). Here's a minimal repro:

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

* In this case an ImageBrush would give the correct results, but in my program, I'm dealing with a DrawingBrush with a custom GeometryDrawing.

I've tried using a pen instead of a brush, changing the TileMode, setting the stretch to Uniform, changing AlignmentX and AlignmentY... nothing seems to work. The origin of the brush is never set to the origin of the rectangle.

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

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

发布评论

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

评论(2

彼岸花ソ最美的依靠 2024-12-06 14:41:15

我使用的解决方案是使用 翻译转换。因此,

dc.DrawRectangle(_brush, null, new Rect(70, 70, 100, 150));

您可以使用:

TranslateTransform transform = new TranslateTransform(70, 70);
transform.Freeze();
dc.PushTransform(transform);
dc.DrawRectangle(_brush, null, new Rect(0, 0, 100, 150));
dc.Pop();

这并不理想,因为每次绘制新矩形时都必须分配一个新的 TranslateTransform(以及另一个底层 DUCE 资源),但它执行在我的测试中足够好。

重要提示:在重新渲染时(例如滚动时),您可能仍会遇到一些奇怪(但微妙)的扭曲。这些可能是由子像素渲染系统引起的。您可以使用 GuidelineSet 或只需将起点的 X 和 Y 舍入到最接近的整数值即可。

The solution I used was to render it at (0,0) with a TranslateTransform. So instead of...

dc.DrawRectangle(_brush, null, new Rect(70, 70, 100, 150));

You can use:

TranslateTransform transform = new TranslateTransform(70, 70);
transform.Freeze();
dc.PushTransform(transform);
dc.DrawRectangle(_brush, null, new Rect(0, 0, 100, 150));
dc.Pop();

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.

柠檬色的秋千 2024-12-06 14:41:15

根据我的经验,平铺画笔非常依赖于 ViewboxViewboxUnitsViewportViewportUnits 属性之间的关系刷子。

单位的两个属性都应设置为 AbsoluteViewportViewbox 应该具有相同的大小(99% 的情况)。

详细地说,这不是我读过的“官方”内容,它只是来自(大量)观察:Viewbox 是渲染绘图时考虑的大小。如果未设置,则路径的Data 将确定Viewbox 大小。另一方面,Viewport 是平铺画笔时考虑的大小。

Viewbox 设置为小于 Viewport 的尺寸会在画笔的每个“图块”之间引入间距。将 Viewbox 设置为大于 Viewport 将导致“图块”重叠(从逻辑上讲,在屏幕上“图块”似乎被剪切)。

From my experience, tiling brushes is very dependent on the relation between Viewbox, ViewboxUnits, Viewport and ViewportUnits properties of the brush.

Both properties for the units should be set to Absolute; the Viewport and Viewbox 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, the Data of the path will determine the Viewbox size. On the other hand, Viewport is the size considered while tiling the brush.

Setting the Viewbox to smaller size than Viewport will introduce spacing between each of the 'tiles' of the brush. Setting the Viewbox larger than Viewport will cause the 'tiles' to overlap (logically, on the screen the 'tile' will seem to be cut).

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