Domanda

I'm trying to render a Visiblox chart to an image to be able to show hundreds of them at the same time. The chart is generated in code and rendered without showing it.

The problem I'm facing is that the chart looks empty after rendering it. The upper control is the chart, the bottom one is the chart rendered before adding it to the control. Some rendering is being done as the watermark is there.

The chart is being populated using bindings and datacontext (just in case it matters). Looking at the chart before rendering it looks like it's effectively empty, thanks makes me ask "When are the bindings retrieved by a UserControl?".

And this is the code I'm using to render the image:

    public BitmapSource Render(FrameworkElement control)
    {
        if (control == null)
        {
            throw new ArgumentNullException("control");
        }

        control.UpdateLayout();

        Viewbox viewBox = new Viewbox();
        viewBox.Child = control; // Control to render
        viewBox.Measure(new System.Windows.Size(control.Width, control.Height));
        viewBox.Arrange(new Rect(0, 0, control.Width, control.Height));
        viewBox.ApplyTemplate();
        viewBox.UpdateLayout();

        RenderTargetBitmap renderer = new RenderTargetBitmap((int)(control.Width * _dpiX / 96.0), (int)(control.Height * _dpiY / 96.0), _dpiX, _dpiY, PixelFormats.Pbgra32);

        DrawingVisual drawingVisual = new DrawingVisual();
        using (DrawingContext drawingContext = drawingVisual.RenderOpen())
        {
            VisualBrush visualBrush = new VisualBrush(viewBox);
            drawingContext.DrawRectangle(visualBrush, null, new Rect(new Point(), new System.Windows.Size(control.Width, control.Height)));
        }

        renderer.Render(drawingVisual);

        // Remove the container to be able to reuse the control again if we appended it to a viewbox
        viewBox.Child = null;

        return renderer;
    }

enter image description here

È stato utile?

Soluzione

If you are interested in saving a Visiblox chart to an image, there is some sample code over at the Visiblox blog. You can find the article here - http://www.visiblox.com/blog/2011/09/saving-a-chart-as-an-image

The article instructs you on how to export to an image in Silverlight, but the download contains some sample code that shows you how to do it in WPF too.

I've used this technique in my applications previously, and I always found that using that sample code was the best place to start!

Let me know if it doesn't work out for you - I have quite a lot of experience at this! My tips would be to make sure you have called Invalidate() on the chart at the correct time, and make sure that it's layout has been updated before you try to export it.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top