locked
Use RenderTargetBitmap with a canvas containing lines and path

    Question

  • Hello,

    I'm trying to render an image using RenderTargetBitmap.RenderAsync function. My image contains a background image and a canvas containing multiple Line and Path object. The Line and Path objects are created programatically and insert in the canvas like that:

    Line line = new Line()
    {
       X1 = x1,
       Y1 = y1,
       X2 = x2,
       Y2 = y2,
       StrokeThickness = Thickness,
       Stroke = SelectedBrush
    };
    
    if (inkManager.Mode == InkManipulationMode.Inking)
       drawCanvas.Children.Add(line);
    else
       drawCanvas.Children.Add(path);

    I have seen in different website that If I insert object programatically, I need to use the Measure and Arrange function to force element size. So, I wrote that:

    Size canvasSize = this.drawCanvas.RenderSize; Windows.Foundation.Point defaultPoint = new Windows.Foundation.Point(0.0,0.0); this.drawCanvas.Measure(canvasSize); this.drawCanvas.Arrange(new Rect(defaultPoint, canvasSize)); this.drawCanvas.UpdateLayout();

    RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
    await renderTargetBitmap.RenderAsync(drawCanvas);

    Unfortunately, the RenderAsync function throw an exception with the message: Value does not fall within excepted range.

    Any idea where the problem comes from?

    Thursday, April 17, 2014 2:39 PM

Answers

  • Yes, the Popup is the problem. The rendered content needs to be in the main window's visual tree. Please see the remarks in the RenderTargetBitmap documentation :

    Content that's not directly connected to the XAML visual tree and the content of the main window won't be captured. This includes Popup content, which is considered to be like a sub-window

    --Rob

    • Marked as answer by BatRocchia Friday, April 18, 2014 2:16 PM
    Friday, April 18, 2014 2:14 PM
    Owner

All replies

  • In what context are you calling this? RenderTargetBitmap requires access to the renderer and can't be called non-UI contexts.

    Your code snippet works fine for me when called from a button Click event.

    --Rob

    Thursday, April 17, 2014 7:12 PM
    Owner
  • I just checked, and my code is called in a button Click event.

    I simplify the code as much as possible, and the canvas can't be rendered even if it has no children. I also try to render an other UI element (an image) and it doesn't work.

    This code is in a Control which is in a class that inherit from ContentController and IPopup. Could the error come from IPopup?


    • Edited by BatRocchia Friday, April 18, 2014 2:17 PM No code needed
    Friday, April 18, 2014 9:16 AM
  • Yes, the Popup is the problem. The rendered content needs to be in the main window's visual tree. Please see the remarks in the RenderTargetBitmap documentation :

    Content that's not directly connected to the XAML visual tree and the content of the main window won't be captured. This includes Popup content, which is considered to be like a sub-window

    --Rob

    • Marked as answer by BatRocchia Friday, April 18, 2014 2:16 PM
    Friday, April 18, 2014 2:14 PM
    Owner