ron.kagan Posted July 14, 2010 Report Share Posted July 14, 2010 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 ? Quote Link to comment Share on other sites More sharing options...
JuanC Posted July 16, 2010 Report Share Posted July 16, 2010 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); rtb.Render(yourControlGoesHere); pngEncoder.Frames.Add(BitmapFrame.Create(rtb)); pngEncoder.Save(stream); stream.Close(); JuanC Quote Link to comment Share on other sites More sharing options...
ron.kagan Posted July 18, 2010 Author Report Share Posted July 18, 2010 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 ? Quote Link to comment Share on other sites More sharing options...
JuanC Posted July 19, 2010 Report Share Posted July 19, 2010 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. JuanC Quote Link to comment Share on other sites More sharing options...
ron.kagan Posted July 20, 2010 Author Report Share Posted July 20, 2010 Solved. this settings is what was needed ,and it will help us later when we need to export chart w/o it ever being loaded so I will use the RendedBeforeLoad settings. Thank you. Quote Link to comment Share on other sites More sharing options...
JuanC Posted July 20, 2010 Report Share Posted July 20, 2010 Glad it helped. The RenderBeforeLoad setting was done to tweak a different behavior, I am not sure if it will help you to export a chart that has never been rendered. JuanC Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.