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
<syncfusion:SfDiagram x:Name="diagram"> <syncfusion:SfDiagram.Background> <ImageBrush ImageSource="/Assets/BackgroundImage.jpg" Stretch="Uniform" AlignmentX="Left" AlignmentY="Top" /> </syncfusion:SfDiagram.Background> |
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
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