Hello,
I use
SfDiagram to add some shapes on pictures taken by the users through the camera.
But for exporting the result, I use the method given by
Martin Kurek there:
Export uwp diagram to imageThe method
GenerateImage() looks like this:
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;
} |
But I encounter an exception in some cases, when I use this method for the second time:
- product page: the user clicks on "Add" to add a first photo => OK
- camera page: the user takes a photo with the camera => OK
- diagram's page: the user edit the photo by adding shapes => OK
- diagram's page: the user exports the image => OK
- product page: the first image has been added => OK
- product page: the user clicks on "Add" to add a second photo => OK
- camera page: the user takes a photo with the camera => OK
- diagram's page: the user edit the photo by adding shapes => OK
- diagram's page: the user exports the image => exception
The exception occurs on "RenderTargetBitmap.RenderAsync" :
Would you have any idea?
Could it be a problem of Dispose when the user leaves the diagram's page for the first time?
Regards,