문제

I have a refrence to a FrameworkElement named _frameworkElement I need to draw a FrameworkElement on a drawingContext at the OnRender event.

Like this:

protected override void OnRender(DrawingContext drawingContext)
{
   drawingContext. ??
   base.OnRender(drawingContext);
}

What I need is to consider any render transform applied to the _frameworkElement

Any clean solution for this problem ? Thanks

EDIT

Why would I need to override OnRender:

As I have a graphical application, user can draw shapes, and select multiple shapes using a selection tool that would draw rectangle selection area.

What I do is I re-parent selected shapes from stage Canvas to a selection Canvas which user can move and resize, after transformation on selection Canvas, user will click on stage Canvas, then I re-parent shapes to the stage Canvas.

The problem:

There is a bottleneck when removing children from Canvas to Canvas, Children.Remove & Children.Add will cost time to implement, specially when user selects large number of shapes to transform.

So ?

I thought not to re-parent selected shapes, instead draw them on the drawingContext of the selection Canvas by overriding OnRender

도움이 되었습니까?

해결책

No I don't believe you can do that nor does it really make sense. The DrawingContext is unique to each UIElement and the framework handles calling the appropriate drawing methods as they're being enumerated (e.g. each element has it's own OnRender pass).

I'm not entirely following your design + problem, but maybe these will help?

WPF canvas performance- children.add called many times

How to draw line of ten thousands of points with WPF within 0.5 second?

If you need to deep-dive into how WPF renders, this is a good read.

다른 팁

Calling Code:

private FrameworkElement _frameworkElement = null;

protected override void OnRender(DrawingContext dc)
{
    var rect = new Rect(new Point(0, 0), new Size(Width, Height));

    dc.DrawImage(UtilityWPF.RenderControl(_frameworkElement, Width.ToInt_Round(), Height.ToInt_Round(), false), rect);
}

Helper Method:

public const double DPI = 96;

/// <summary>
/// This tells a visual to render itself to a wpf bitmap
/// </summary>
/// <remarks>
/// This fixes an issue where the rendered image is blank:
/// http://blogs.msdn.com/b/jaimer/archive/2009/07/03/rendertargetbitmap-tips.aspx
/// </remarks>
public static BitmapSource RenderControl(FrameworkElement visual, int width, int height, bool isInVisualTree)
{
    if (!isInVisualTree)
    {
        // If the visual isn't part of the visual tree, then it needs to be forced to finish its layout
        visual.Width = width;
        visual.Height = height;
        visual.Measure(new Size(width, height));        //  I thought these two statements would be expensive, but profiling shows it's mostly all on Render
        visual.Arrange(new Rect(0, 0, width, height));
    }

    RenderTargetBitmap retVal = new RenderTargetBitmap(width, height, DPI, DPI, PixelFormats.Pbgra32);

    DrawingVisual dv = new DrawingVisual();
    using (DrawingContext ctx = dv.RenderOpen())
    {
        VisualBrush vb = new VisualBrush(visual);
        ctx.DrawRectangle(vb, null, new Rect(new Point(0, 0), new Point(width, height)));
    }

    retVal.Render(dv);      //  profiling shows this is the biggest hit

    return retVal;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top