<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Jaime Rodriguez  : RenderTargetBitmap</title><link>http://blogs.msdn.com/jaimer/archive/tags/RenderTargetBitmap/default.aspx</link><description>Tags: RenderTargetBitmap</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>RenderTargetBitmap tips</title><link>http://blogs.msdn.com/jaimer/archive/2009/07/03/rendertargetbitmap-tips.aspx</link><pubDate>Fri, 03 Jul 2009 22:33:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9817000</guid><dc:creator>jaimer</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/jaimer/comments/9817000.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaimer/commentrss.aspx?PostID=9817000</wfw:commentRss><description>&lt;p&gt;Quite often,&amp;nbsp; I run into the “RenderTargeBitmap gave me an empty image” or “RenderTargetBitmap did not render my visual”..&amp;nbsp;&amp;nbsp; &lt;br&gt;Hint: this happens a lot to people putting stuff into StackPanels. &lt;/p&gt; &lt;p&gt;The problem is that renderTargetBitmap is working at the Visual layer to render the visual and to do the rendering, it looks at the local properties of the visual (as expected). &lt;br&gt;Layout happens at a different (higher) layer in the platform but often ‘parents’ to your visual will apply offsets or transforms to your visual to position it, since the parent is setting these properties on your actual visual, these properties will be picked up by RenderTargetBitmap logic. &lt;br&gt;&lt;br&gt;The best (and simplest) workaround I have seen came from Adam Smith ( WPF team lead and reliable WPF know it all). It simply wraps the Visual into a VisualBrush that it then draws into a DrawingContext. &lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;BitmapSource &lt;/span&gt;CaptureScreen(&lt;span style="color: #2b91af"&gt;Visual &lt;/span&gt;target, &lt;span style="color: blue"&gt;double &lt;/span&gt;dpiX, &lt;span style="color: blue"&gt;double &lt;/span&gt;dpiY)
{
    &lt;span style="color: blue"&gt;if &lt;/span&gt;(target == &lt;span style="color: blue"&gt;null&lt;/span&gt;)
    {
        &lt;span style="color: blue"&gt;return null&lt;/span&gt;;
    }
    &lt;span style="color: #2b91af"&gt;Rect &lt;/span&gt;bounds = &lt;span style="color: #2b91af"&gt;VisualTreeHelper&lt;/span&gt;.GetDescendantBounds(target);
    &lt;span style="color: #2b91af"&gt;RenderTargetBitmap &lt;/span&gt;rtb = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;RenderTargetBitmap&lt;/span&gt;((&lt;span style="color: blue"&gt;int&lt;/span&gt;)(bounds.Width * dpiX / 96.0),
                                                    (&lt;span style="color: blue"&gt;int&lt;/span&gt;)(bounds.Height * dpiY / 96.0),
                                                    dpiX,
                                                    dpiY,
                                                    &lt;span style="color: #2b91af"&gt;PixelFormats&lt;/span&gt;.Pbgra32);
    &lt;span style="color: #2b91af"&gt;DrawingVisual &lt;/span&gt;dv = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DrawingVisual&lt;/span&gt;();
    &lt;span style="color: blue"&gt;using &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;DrawingContext &lt;/span&gt;ctx = dv.RenderOpen())
    {
        &lt;span style="color: #2b91af"&gt;VisualBrush &lt;/span&gt;vb = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;VisualBrush&lt;/span&gt;(target);
        ctx.DrawRectangle(vb, &lt;span style="color: blue"&gt;null&lt;/span&gt;, &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Rect&lt;/span&gt;(&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Point&lt;/span&gt;(), bounds.Size));
    }
    rtb.Render(dv);
    &lt;span style="color: blue"&gt;return &lt;/span&gt;rtb;
}&lt;/pre&gt;
&lt;p&gt;I like that workaround the best, and that is the one I use most of the time.&amp;nbsp; &lt;br&gt;I know other people who workaround this same issue by wrapping the visual they are going to RTB in a Border. This abstracts any transforms that were being applied by the original parent, and now RTB works well.&amp;nbsp; I obviously don’t like this one as much since it ‘imposes’ on my Visual Trees.&amp;nbsp; Still, it works, so it is a choice. &lt;br&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Here is a screenshot of a &lt;a href="http://www.cookingwithxaml.com/recipes/rendertargetbitmap/RenderTargetBitmapSample.zip"&gt;sample&lt;/a&gt; demonstrating the issue and the solutions:&lt;/p&gt;
&lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px 0px 0px 10px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" align="right" src="http://blogs.msdn.com/blogfiles/jaimer/WindowsLiveWriter/RenderTargetBitmaptips_975E/image_thumb.png" width="315" height="310"&gt;&lt;br&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;The first column is the original images and buttons. They are all in a StackPanel.&amp;nbsp; The top 3 items in this panels are not wrapped inside a border, the latter 3 are wrapped in a border. &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The second column, shows what you would get if you just use RenderTargetBitmap.&amp;nbsp; Notice that the button and second image are missing; the reason is cause the StackPanel is offsetting them and RTB is picking that up.&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br&gt;Do notice that even with no special workaround, the ones wrapped in the border worked OK. &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The third column shows all the items rendered with the workaround above. Note that it works for all items, including the ones that had border. &lt;/p&gt;
&lt;p&gt;&amp;nbsp; &lt;/p&gt;
&lt;p&gt;The source code for this small sample (including the workaround) is &lt;a href="http://www.cookingwithxaml.com/recipes/rendertargetbitmap/RenderTargetBitmapSample.zip"&gt;here&lt;/a&gt;. &lt;br&gt;&lt;br&gt;While I am into RenderTargetBitmap, a reminder to look at this &lt;a href="http://support.microsoft.com/kb/967634/"&gt;KB article (and or QFE).&lt;/a&gt; &lt;br&gt;In 3.5 SP1, we fixed a few leaks for RenderTargetBitmap, but there is still one open; it has to do with doing RenderTargetBitmap of a 3D scene that has VisualBrushes inside the scene. &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9817000" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaimer/archive/tags/Windows+Presentation+Foundation/default.aspx">Windows Presentation Foundation</category><category domain="http://blogs.msdn.com/jaimer/archive/tags/RenderTargetBitmap/default.aspx">RenderTargetBitmap</category></item></channel></rss>