Export uwp diagram to image
You provides otherwise on the website:
https://www.syncfusion.com/products/uwp/diagram#exporting-and-printing
Martin
I have found workaround, I have used RenderTargetBitmap.RenderAsync on instance SfDiagram, work fine, but this solution has restriction: diagram must be in Visual Tree!
protected async Task<IRandomAccessStream> GenerateImage(UIElement diagram)
{
// Render to an image at the current system scale and retrieve pixel contents
var renderTargetBitmap = new RenderTargetBitmap();
// unselect all nodes
ObservableCollection<TraNode> nodes = ((diagram as SfDiagram).Nodes as ObservableCollection<TraNode>);
if(nodes != null && nodes.Count > 0)
{
foreach (var node in nodes)
{
node.IsSelected = false;
}
}
await renderTargetBitmap.RenderAsync(diagram);
var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();
var pixelBytes = new byte[pixelBuffer.Length];
using (var reader = DataReader.FromBuffer(pixelBuffer))
{
reader.ReadBytes(pixelBytes);
}
IRandomAccessStream stream = new InMemoryRandomAccessStream();
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
encoder.SetPixelData(
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Straight,
(uint)renderTargetBitmap.PixelWidth,
(uint)renderTargetBitmap.PixelHeight,
DisplayInformation.GetForCurrentView().LogicalDpi,
DisplayInformation.GetForCurrentView().LogicalDpi,
pixelBytes);
await encoder.FlushAsync();
return stream;
}
I have used this method to export image from diagram layers as PNG to PDF, here is a result:

Martin
I tried to reproduce your solution, but I don't see what is the "TraNode" object: could you help me?
Thanks,
public class TraNode : Syncfusion.UI.Xaml.Diagram.Node
{
public Guid Id { get; set; }
}
Please let us know if you need any other assistance.
Your solution works very fine in many cases, thank you!
But I would like to know how you have setted your background image?
- as a Node of the SfDiagram
- though the PageBackground of the PageSettings
For me, the best way seems be to use the Background property:
<syncfusion:SfDiagram x:Name="diagram"> <syncfusion:SfDiagram.Background> <ImageBrush ImageSource="/Assets/BackgroundImage.jpg" Stretch="Uniform" AlignmentX="Left" AlignmentY="Top" /> </syncfusion:SfDiagram.Background> |
=> the problem is that your code takes a "screenshot" of all the the SfDiagram's area, so if the background image is smaller, there are black or white stripes below or next to the image. is there a way to manage this?
Hi Pierre,
<Grid x:Name="RenderedGrid" Grid.Row="0" Grid.Column="0" Margin="0,60" Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="542"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="534"/>
</Grid.RowDefinitions>
<Image Grid.Row="0" Grid.Column="0" Source="/Assets/body-normal.jpg" Width="542" x:Name="RenderTeloNormal"/>
<controls:Diagram Grid.Row="0" Grid.Column="0" Background="Transparent" Width="542" Height="534"
x:Name="diagram" HorizontalAlignment="Left" Tool="SingleSelect"
Nodes="{Binding Nodes, Mode=TwoWay}"
IsEnabled="{Binding TraModel.SineEnb, Converter={StaticResource FalckNullToBooleanConverter}}">
</controls:Diagram>
</Grid>
I have exported only png of objects from sfDiagram added from stencil, background Image I have added manualy to PDF:
Stream imageBodyEmptyStream = typeof(NahladPageViewModel).GetTypeInfo().Assembly.GetManifestResourceStream("Falck.UILogic.Assets.body-baw.jpg");
var imageBodyEmpty = new PdfBitmap(imageBodyEmptyStream);
and than:
// Replace the first image in the page.
//document.Pages[0].ReplaceImage(0, bmp);
// NO Replace in UWP: https://www.syncfusion.com/forums/125998/how-to-replace-an-image,
// but in documentation: https://help.syncfusion.com/file-formats/pdf/working-with-images#replacing-images-in-an-existing-pdf-document
document.Pages[0].Graphics.DrawImage(imageBodyEmpty, 185, 300, 125, 125);
document.Pages[0].Graphics.DrawImage(imageBodyObjectsFromStencil, 172, 288, 150, 150);
Martin
The exception occurs on "RenderTargetBitmap.RenderAsync" :


Hi Pierre,
Exception “Value does not fall within the expected range” means that rendered element instance in your case “diagram” is not in actual Visual Tree (you have to see diagram when you are exporting).
Martin- 16 Replies
- 3 Participants
-
MK Martin Kurek
- Sep 21, 2016 10:49 PM UTC
- Jan 12, 2017 10:50 AM UTC