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

Render Chartview to image from windows service

1 Answer 84 Views
ChartView
This is a migrated thread and some comments may be shown as answers.
Datafyer
Top achievements
Rank 1
Veteran
Datafyer asked on 16 Apr 2015, 07:25 PM

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));
    view.UpdateLayout();
 
    var rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Default);
    rtb.Render(view);
 
    var png = new PngBitmapEncoder();
    png.Frames.Add(BitmapFrame.Create(rtb));
    var memStream = new MemoryStream();
    png.Save(memStream);
 
    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;
window.Show();
window.Close();
 
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?

1 Answer, 1 is accepted

Sort by
0
Petar Marchev
Telerik team
answered on 20 Apr 2015, 08:42 AM
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.

Regards,
Petar Marchev
Telerik
 

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

 
Tags
ChartView
Asked by
Datafyer
Top achievements
Rank 1
Veteran
Answers by
Petar Marchev
Telerik team
Share this question
or