Jump to content
Software FX Community

Exporting to image


Recommended Posts

Hello there.

In my application I have multiple chart elements in different tabs. one scenario demands me to export all currently opened charts to images.

however doing so results in black empty images for all charts that are not currently visible to the eye (visibility is visible but they are in a tab that is not the current active tab).

Can you think of a reason a chart will export to a blank image ? Do you need more information on this issue ?

Link to comment
Share on other sites

Unfortunately in WPF controls do not "paint" themselves as before (Win32 and other environments) but instead they generate visuals in a retained mode, so generating an image depends on creating those visuals.

We have some code that will try to render charts that have not been painted but it only does so when called on a backround thread, please try creating a thread, setting the IsBackground property to see if this makes any difference.

To generate the image we eventually rely on RenderTargetBitmap to take a visual and generate an image, Note that if you try to use this sample code it will generate a correct image if the control has been rendered but will generate a black image if the control has not been rendered yet.

Stream stream = File.Create("C:\\Temp\\Button.png");

PngBitmapEncoder pngEncoder = new PngBitmapEncoder();

RenderTargetBitmap rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);






Link to comment
Share on other sites

for any wpf control, the code for export a visual to image works, however for your chart element , it does not.

doing the solution with the thread first gave me the cross thread violation , and using the dispatcher invoke, the result was that the image was not even black - it was completely empty.

Can you think of another path for solving this critical issue ?

Link to comment
Share on other sites

I can easily replicate the blank image with any WPF control (e.g. a button) if the code runs before the particular tab where the control lives is ever displayed.

Once a tab is visited, and no longer the current tab, other WPF controls will keep their visuals but we found that for the chart it made more sense to tear down those visuals to keep memory usage at a minimum. You can override this behavior with the following code

chart1.AdvancedSettings |= ChartSettings.KeepVisualsOnUnload;

You should not need to use any threading code for this to work.



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.

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.

  • Create New...