WPF:InvalidateVisual不会由OnRender绘制。 [英] WPF: InvalidateVisual doesn't draw by OnRender.

查看:218
本文介绍了WPF:InvalidateVisual不会由OnRender绘制。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:使用OnRender通过RenderTargetBitmap绘制到ImageBrush。

问题:它没有绘制。

问题:如何使用OnRender而不添加控件作为Canvas.Children的孩子?

注意:

1.如果要将Control作为子项添加到Canvas.Children,它可以工作。您可以通过关闭注释来完成。

2.即使不调用InvalidateVisual,如果使用VisualCollection也可以。

3.目标是使用OnRender而不使用OnRender将Control作为子项添加到Canvas.Children。

代码:

Goal: To use OnRender to draw into ImageBrush by RenderTargetBitmap.
Problem: It doesn't draw.
Question: How to do it using OnRender without adding Control as child to Canvas.Children?
Notes:
1. It works, if to add Control as child to Canvas.Children. You can do by turning the comment off.
2. It works even without calling InvalidateVisual, if to use VisualCollection.
3. The goal is to do it using OnRender without adding Control as child to Canvas.Children.
Code:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        Canvas canvas = new Canvas();
        this.Content = canvas;
        canvas.Background = Brushes.Black;

        MyImage myimage = new MyImage();
        myimage.InvalidateMeasure();
        myimage.InvalidateArrange();
        myimage.InvalidateVisual();
        myimage.UpdateLayout();

        //canvas.Children.Add(myimage); // Works

        canvas.MouseDown += delegate
        {
            RenderTargetBitmap target = new RenderTargetBitmap(100, 100, 96, 96, PixelFormats.Default);
            target.Render(myimage);

            ImageBrush brush = new ImageBrush();
            brush.ImageSource = target;
            canvas.Background = brush;
        };

    }
}

class MyImage : Image
{
    protected override void OnRender(DrawingContext dc)
    {
        base.OnRender(dc);

        dc.DrawRectangle(Brushes.Red, null, new Rect(0, 0, 100, 100));
    }
}

推荐答案

在添加视觉效果之前,OnRender不会被调用一个可视树。

如果目标是在不向可视化树添加视觉的情况下获取图像,则可以使用VisualCollection / DrawingVisual。



代码:

OnRender doesn't get called until a visual gets added into a visual tree.
If the goal is to get the image without adding the visual to the visual tree, you can use VisualCollection/DrawingVisual.

Code:
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        Canvas canvas = new Canvas();
        this.Content = canvas;
        canvas.Background = Brushes.Black;

        MyImage myimage = new MyImage();
        myimage.InvalidateMeasure();
        myimage.InvalidateArrange();
        myimage.InvalidateVisual();
        myimage.UpdateLayout();

        //canvas.Children.Add(myimage); // You no longer need it

        canvas.MouseDown += delegate
        {
            RenderTargetBitmap target = new RenderTargetBitmap(100, 100, 96, 96, PixelFormats.Default);
            target.Render(myimage);

            ImageBrush brush = new ImageBrush();
            brush.ImageSource = target;
            canvas.Background = brush;
        };

    }
}

class MyImage : Image
{
    public MyImage()
    {
        visuals = new VisualCollection(this);
        visual = new DrawingVisual();
        visuals.Add(visual);

        Draw();
    }

    private void Draw()
    {
        using (DrawingContext dc = visual.RenderOpen())
            dc.DrawRectangle(Brushes.Red, null, new Rect(0, 0, 100, 100));
    }

    VisualCollection visuals;
    DrawingVisual visual;

    protected override Visual GetVisualChild(int index)
    {
        return visuals[index];
    }
    protected override int VisualChildrenCount
    {
        get { return visuals.Count; }
    }
}





资料来源:

https://social.msdn.microsoft.com/Forums/en-US/81ccabb8-4f38-46ba-827c-d0a2b66372de/wpf-can-onrender-render-if-a-control-is-没有添加为孩子 [ ^ ]


这篇关于WPF:InvalidateVisual不会由OnRender绘制。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆