RenderTargetBitmap.Render performance

1,602 views
Skip to first unread message

Jeremy Alles

unread,
Apr 6, 2011, 3:59:18 AM4/6/11
to wpf-di...@googlegroups.com
Hi everybody,

I'm working on a WPF diagramming control and I need to have a drag/drop feature with some kind of "ghost" support. When the user starts dragging the current selection, I must display a ghost of the selected objects right under the mouse cursor. I've this feature working since a few weeks and now I'm starting to look at performance.

I'm using the following code to take a snapshot of my control and then I crop it to fit the current selection. This code sample is very popular on the web:

public static BitmapSource CaptureVisual(Visual target, double dpiX, double dpiY)
{
    Rect bounds = VisualTreeHelper.GetDescendantBounds(target);
   
    RenderTargetBitmap rtb = new RenderTargetBitmap((int)(bounds.Width * dpiX / 96.0),
                                                    (int)(bounds.Height * dpiY / 96.0),
                                                    dpiX,
                                                    dpiY,
                                                    PixelFormats.Pbgra32);

    DrawingVisual drawingVisual = new DrawingVisual();
    using (DrawingContext context = drawingVisual.RenderOpen())
    {
        VisualBrush visualBrush = new VisualBrush(target);
        context.DrawRectangle(visualBrush, null, new Rect(new Point(), bounds.Size));
    }

    rtb.Render(drawingVisual);
    return rtb;
}

I'm looking for other implementation which could have better performance. In particular when my control starts being big, the rtb.Render call takes a lot of time.
Do you have any idea ?

Regards,
Jeremy

Mark Smith

unread,
Apr 6, 2011, 8:28:46 AM4/6/11
to wpf-di...@googlegroups.com
Hi Jeremy,

Why do you need the bitmap? Was a VisualBrush (or BitmapCacheBrush)
not sufficient to copy it to a Rectangle? I assume you have an Image
you are dragging around to represent the content... Could it be a
Rectangle instead, filled with a BCB? That should be faster..

Mark

Jeremy Alles

unread,
Apr 6, 2011, 8:37:01 AM4/6/11
to wpf-di...@googlegroups.com, Mark Smith
Mark,

I'm still running 3.5 SP1 for this application so I don't have access the new features (BitmapCacheBrush) but it's something I'll considerer as soon as we switch to 4.0. Actually I'm not sure I need a bitmap but I do not need the "live update" feature of a VisualBrush neither. Because I'm drawing some stuff in the adorner layer during drag'n'drop and I must make sure those drawings are not in the ghost.

I'm going to check if a VisualBrush could work. Thanks for the feedback :-)

Jeremy

Tamir Khason

unread,
Apr 7, 2011, 2:52:03 AM4/7/11
to wpf-di...@googlegroups.com
Well,
A while ago i did big research about graphical performance of WPF.
Here some information which might be useful for you:
http://khason.net/blog/performance-appliance-of-rendertargetbitmap/
http://khason.net/blog/how-to-high-performance-graphics-in-wpf/

--
Tamir http://khason.net/

Peter O'Hanlon

unread,
Apr 7, 2011, 3:23:08 AM4/7/11
to Tamir Khason, wpf-di...@googlegroups.com
That's a really nice article.

Jeremy Alles

unread,
Apr 7, 2011, 3:24:48 AM4/7/11
to wpf-di...@googlegroups.com, Peter O'Hanlon, Tamir Khason
Yes thank you Tamir, nice posts. I'm not yet sure the way I'm going to choose. It looks like there no optimization possible when using RenderTargetBitmap. I'm going to see if VisualBrush could work.
Reply all
Reply to author
Forward
0 new messages