We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

ArgumentException on RenderAsync

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 image

The 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" :
Exception
Details

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,

1 Reply

KR Keerthivasan Ramamoorthy Syncfusion Team January 10, 2017 09:28 AM UTC

Hi Pierre,  
Reported issue:” The Value does not fall within the expected range”. 
 
We have analyzed your reported issue. The RenderTargetBitmap can't render something which is not in the visual tree, or in popup, etc. Please refer the msdn link for the details from “XAML visuals and RenderTargetBitmap capture capabilities” title
 
  
Regards,  
Keerthivasan R.  


Loader.
Live Chat Icon For mobile
Up arrow icon