Render Chartview to image from windows service

2 posts, 0 answers
  1. Patrick
    Patrick avatar
    55 posts
    Member since:
    Jun 2008

    Posted 16 Apr 2015 Link to this post

    I have a windows service that periodically sends emails with images of ChartView controls. I am having a hard time getting the ChartView to fully render unless I attach it to a window and show the window and then immediately close it. The code is running from a windows service and so this is really not desired.

     If I render the control using the following BuildContentStream method then it simply displays the "No data to display message"


    private MemoryStream BuildContentStream(FrameworkElement visualItem, int width, int height)
        var view = visualItem;
        view.Measure(new Size(width, height));
        view.Arrange(new Rect(0, 0, width, height));
        var rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Default);
        var png = new PngBitmapEncoder();
        var memStream = new MemoryStream();
        memStream.Position = 0;
        return memStream;

    If I attach the control to a window and then do the following then it works correctly.

    // The window acts as a visualizer. Can't get it working otherwise
    var window = new RadWindow();
    window.Height = 300;
    window.Width = 400;
    window.Content = errorItem.View;
    window.WindowState = WindowState.Normal;
    var stream = BuildContentStream(errorItem.View, 400, 300);


    I have also tried the ExportToImage extension without success?

    Is it possible to get this working from a windows service without any UI?

  2. Petar Marchev
    Petar Marchev avatar
    968 posts

    Posted 20 Apr 2015 Link to this post

    Hello Patrick,

    The chartview is a UI control that relies on the wpf infrastructure. The framework does many things in order to prepare and lay out the visual elements. This includes, but is not limited to initializing, measuring and arranging. None of our UI controls have been built with the intent of them being exported without being attached to a visual tree and let be fully laid out by the framework. We usually suggest that our clients let the elements get properly rendered by the framework and get exported after that, when the final user has seen them.

    I hope you understand that this is not the purpose of the chart control and we never claimed that you can export it without having it attached to the visual tree. Anyway, in most cases we have been successful to find a way to get the chart to correctly export. I am attaching a small project that demonstrates this. Do check it out and see if you can find the differences with the code you have. In essence, the PrepareChartForExport method is the one that handles manually what the framework would have otherwise handled. It checks the chart's size, measures it, arranges it and updates the layout.

    I can see that you have similar code, but you can see that I have it working in the demo. I also tried using the export code you provided and still the charts were properly exported. If you are unable to resolve this on your own, please modify the project so that the export issue is reproduced and send it back to us so that we can investigate and see if there is something we can do.

    Petar Marchev

    See What's Next in App Development. Register for TelerikNEXT.

  3. DevCraft R3 2016 release webinar banner
Back to Top