Copying 3D graph to clipboard does not copy axis labels

1 Answer 105 Views
Chart
Martijn
Top achievements
Rank 1
Martijn asked on 10 Nov 2022, 02:44 PM

I use a RadCartesianChart3D for displaying a 3D graph. For copying this graph to Clipboard, I render the view model again and copy it to the Clipboard. But that copy does not contain the axis labels.

I use the following code to copy the view model to Clipboard:

            var data = new DataObject();

            var bitmap = RenderToBitmap(this);
            if (bitmap != null)
            {
                data.SetData(DataFormats.Bitmap, bitmap);
            }

            Clipboard.SetDataObject(data);

And RenderToBitmap is as follows:

        public BitmapSource RenderToBitmap(object viewModel, double dpi = 300.0)
        {
            var element = new Border()
            {
                Child = new ContentControl()
                {
                    Content = viewModel,
                },
                Background = new SolidColorBrush(Colors.White),
            };

            element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
            element.Arrange(new Rect(element.DesiredSize));
            element.InvalidateVisual();
            element.UpdateLayout();

            var target = new RenderTargetBitmap((int)(element.ActualWidth * dpi / 96.0), (int)(element.ActualHeight * dpi / 96.0), dpi, dpi, PixelFormats.Default);

            target.Render(element);

            return BitmapFrame.Create(target);
        }

 

Why is the copy not containing the axis labels?

1 Answer, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 11 Nov 2022, 01:09 PM

Hello Martijn,

We are aware of this behavior. It happens because the chart needs to be placed and loaded in the visual tree, in order to render its axes. This means that you cannot export the chart to an image until you render it. One way to achieve your requirement is to show the chart in a hidden Window, export it and then hide the window. For example:

var w = new Window();
w.WindowStyle = WindowStyle.None;
w.Height = 0;
w.Width = 0;
w.Content = elementHostingTheChart;
w.Loaded += (s, e) =>
{
    using (Stream stream = new FileStream("../../test.png", FileMode.Create))
    {
        Telerik.Windows.Media.Imaging.ExportExtensions.ExportToImage(
            elementHostingTheChart, stream, new System.Windows.Media.Imaging.PngBitmapEncoder());
    }
    w.Close();
};
 
w.Show();

This approach is mentioned also here.

I hope that helps.

Regards,
Martin Ivanov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Martijn
Top achievements
Rank 1
commented on 16 Nov 2022, 07:30 AM

Hello Martin,

 

That solves a big part from my problem. But then the saved picture looks as attached. What parts do I need to bind to the view model to make sure that it is saved as visible on the screen?

Martin Ivanov
Telerik team
commented on 18 Nov 2022, 08:13 AM

You should ensure that the panel (that hosts the legend and the chart) you are exporting provides enough space (width and height) for the elements. I will guess that the layout shown in your first picture (graph-original.png) provides more width and height, than the layout that is passed to the export method. So, using bigger width and height for the measure should do the job. Also, you can try changing the Distance of the chart's camera behavior, so the chart appears farther away, thus smaller.
Martijn
Top achievements
Rank 1
commented on 05 Dec 2022, 02:28 PM

Hello Martin,

 

Sorry for the late reply, but you were right. I changed width/height settings for the exported version and now it is displaying correctly!

Tags
Chart
Asked by
Martijn
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Share this question
or