This is a migrated thread and some comments may be shown as answers.

Why the image exported from diagram is 0KB?

2 Answers 74 Views
Diagram
This is a migrated thread and some comments may be shown as answers.
Jonathan
Top achievements
Rank 1
Jonathan asked on 12 Jun 2017, 08:06 AM

Hi, I has a diagram, and use ExportToImage method to export to png, but the png file is 0KB, and can't diaplay in windows photo viewer, why?

The code is:

namespace OpenDiagramdemo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public RadDiagram diagram;
        public MainWindow()
        {
            InitializeComponent();
            diagram = new RadDiagram();
            //the shapes up to 500, display is slow
            for(int i = 0; i < 1000; i++)
            {
                RadDiagramShape s = new RadDiagramShape();
                s.Stroke = Brushes.Red;
                s.BorderBrush = Brushes.Red;
                s.StrokeThickness = 2;
                s.IsEditable = false;
                s.Foreground = Brushes.Transparent;
                s.Background = Brushes.Transparent;
                s.Width = 113;
                s.Height = 76;
                s.X = i;
                s.Y = i;
                s.Geometry = ShapeFactory.GetShapeGeometry(CommonShapeType.EllipseShape);//"M0.5,0.5L111.5,0.5 111.5,74.5 0.5,74.5z"
                diagram.AddShape(s);
            }
        }
       
        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            FileStream fs = File.Open("D:\\\\tt.png", FileMode.Create);
            this.diagram.ExportToImage(fs);
            fs.Close();
        }
    }
}

When run this program, make a diagram And draw a menu in xaml. Define the menu click event MenuItem_Click. Click menu and see the result described above.

Thanks!

2 Answers, 1 is accepted

Sort by
0
Accepted
Martin Ivanov
Telerik team
answered on 15 Jun 2017, 08:12 AM
Hello Jonathan,

The diagram will be export properly only if it is measured and arranged before the method call. Otherwise, the image will be empty. The measure and arrange happen automatically when you add the control in the visual tree, but in your case you can do it manually. Here is an example in code:
var userControl = new UserControl();
userControl.Content = diagram;
userControl.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
userControl.Arrange(new Rect(0, 0, diagram.DesiredSize.Width, diagram.DesiredSize.Height));
Note that you will need to add the diagram into a parent control and measure it. This code can be placed just after 'for' loop that populates the diagram.

Regards,
Martin Ivanov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Jonathan
Top achievements
Rank 1
answered on 16 Jun 2017, 10:52 AM

It's OK. Thanks Martin.

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