I want to render an XAML control to a PNG file with the RenderTargetBitmap functionality. This works great so far.
But if the XAML contains any Images these images are not rendered in the PNG file. This is my XAML:
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="LayoutRoot"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="336"
d:DesignWidth="336"
Background="DarkGray">
<TextBlock Text="This is my Text"
FontSize="35"
HorizontalAlignment="Center" />
<Image Source="http://www.myurl.com/image.png"
Height="200" /></Grid>
And this is my code:
private BitmapSource RenderToBitmap(FrameworkElement target)
{
Rect bounds = VisualTreeHelper.GetDescendantBounds(target);
RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)target.ActualWidth,
(int)target.ActualHeight,
96, 96, PixelFormats.Pbgra32);
DrawingVisual visual = new DrawingVisual();
using (DrawingContext context = visual.RenderOpen())
{
VisualBrush brush = new VisualBrush(target);
context.DrawRectangle(brush, null, new Rect(new Point(), bounds.Size));
}
renderBitmap.Render(visual);
return renderBitmap;
}
Also tried with referenced images like "/Assets/image.png" and as Resource. Every time the image is missing in the rendered PNG File.