Jump to content
Software FX Community

Exporting bitmap thumbnails


bspindel
 Share

Recommended Posts

On the windows forms version of chartFX (VS 2005), it is possible to export the chart to an image, even before adding the control to a form. With that, it was possible , for instance , to show a series of chart previews without actually adding each one of them to the form.

When I try to do the same with the WPF version, it doesn't work out the same way. Actually, exporting the chart to a bitmap only seems to work if the control itself had already been rendered visually. Is that correct or am I missing something?

Link to comment
Share on other sites

Because of the way WPF works we have to use some API that requires our visuals to be created (even if they are not visible or parented at the time). We also had to fix some timing issues related to the fact that WPF uses a separate rendering thread.

We have a new build that should work in your scenario, please send a message to wpf at softwarefx dot com with a hotfix request. We have tested the following

Chart chart = new Chart();chart.Measure(new Size(600, 300));chart.Arrange(new Rect(0, 0, 600, 300));chart.Export(FileFormat.Png, "C:\\Temp\\Test.png");

Note that if you do not do measure/arrange we will generate a chart with a default 400x300 size. Also note that we have not yet tested scenarios where you use a single chart to generate multiple images while changing data and/or visual attributes.

Regards,

JuanC

Link to comment
Share on other sites

  • 3 months later...

I have a chart object as a shared varible on an object that I intend to reuse to generate multiple chart images.  The image generation is required to run in a separate thread from the GUI.

 

The proces currently follow this path:

 Chart object instantiated in GUI thread (as framework will not allow me to create it in a non-STA way)

Separate thread fires to process data, populating the chart object (just with title for simplicity).

when chart is populated, call the Export method

All is fine up to the export at which point an InvalidOperationException is thrown, because a different thread owns the object.  Can you suggest any way round this, given that I can add titles to the object quite happily?

 

Many thanks

 

Paul

 

Link to comment
Share on other sites

Unfortunately any classes that derive from Control in WPF (actually the restriction is on DispatcherObject high in the hierarchy) have thread affinity.

Because a big subset of our API does not directly affect our visuals we do not add checks to make sure you are calling from the "right" thread, which is why setting titles works even when called from other threads.

Export will render the chart so at this point WPF throws an exception because of the thread mismatch.

I would recommend you try to create and export the chart in the UI thread while populating it on the worker thread, I understand you wanted to do this on a different thread but I think the "abstraction quickly leaks". I tried for example the following hack

private void OnThread (object sender, EventArgs e){   ThreadStart threadStart = new ThreadStart(ThreadWorker);   Thread thread = new Thread(threadStart);   thread.Name = "Worker";   thread.SetApartmentState(ApartmentState.STA);   thread.Start();}

private void ThreadWorker (){   Chart chart = new Chart();   chart.Width = 400;   chart.Height = 300;   chart.Style = ChartFX.WPF.Motifs.Basic.Style;   List<int> dataValues = new List<int>();   dataValues.Add(2);   dataValues.Add(4);   dataValues.Add(6);   dataValues.Add(1);   dataValues.Add(3);

  chart.ItemsSource = dataValues;

  chart.Export(

FileFormat.Png, "C:\\Temp\\Test.png");}

But quickly found we do a lot of internal caching on templates and other objects and so if you execute the OnThread method twice the internal objects will also be called from the wrong thread, even if you could enforce a single worker thread we had some rendering issues with this approach. We will check if we can enable this scenario but I am not sure if it is possible because of WPF limitations.

Regards,

JuanC

 

Link to comment
Share on other sites

  • 5 weeks later...

We think we have fixed the internal caching issues that were causing thread affinity (some DataTemplates had to be sealed and some freezables were not properly frozen) so you should be able to create and export charts from worker threads. Please note that the threads have to be STA so you cannot use ThreadPool.

We also found that BitmapEffects do not work in this environment so you will have to use the ChartFX.XBAP assemblies instead of ChartFX.WPF.*, our XBAP assemblies do not include bitmap effects because they do not work well in low trust scenarios.

These changes have been included in our beta 3 bits which you can download here.

Regards,

JuanC

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...