Thank you for the question and answer.
In the interest of others seeking the same answer.
I found that Clemens' path leaves a black bar in the image when the image is shifted either down or to the right. As if he did not display the element in the correct position in the bitmap.
So I had to use VisualBrush, as Amar suggested.
Here is the code that worked for me:
RenderTargetBitmap RenderVisual(UIElement elt) { PresentationSource source = PresentationSource.FromVisual(elt); RenderTargetBitmap rtb = new RenderTargetBitmap((int)elt.RenderSize.Width, (int)elt.RenderSize.Height, 96, 96, PixelFormats.Default); VisualBrush sourceBrush = new VisualBrush(elt); DrawingVisual drawingVisual = new DrawingVisual(); DrawingContext drawingContext = drawingVisual.RenderOpen(); using (drawingContext) { drawingContext.DrawRectangle(sourceBrush, null, new Rect(new Point(0, 0), new Point(elt.RenderSize.Width, elt.RenderSize.Height))); } rtb.Render(drawingVisual); return rtb; }
source share