2012-04-18 49 views
0

我试图将Visiblox图表渲染到图像以便能够同时显示数百个图像。该图表在代码中生成并呈现而不显示它。RenderTargetBitmap渲染为空控件(如果未在窗口中设置)(Visiblox图表)

我面临的问题是图表渲染后看起来是空的。上面的控件是图表,最下面的图表是在将图表添加到控件之前呈现的图表。一些渲染正在完成,因为水印在那里。

图表正在使用绑定和datacontext填充(以防万一)。在呈现之前查看图表,看起来它实际上是空的,谢谢让我问“何时由UserControl?检索到绑定”。

这是我使用的呈现图像的代码:

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

回答

1

如果你有兴趣在保存Visiblox图表的图像,有一些示例代码在Visiblox博客。你可以在这里找到这篇文章 - http://www.visiblox.com/blog/2011/09/saving-a-chart-as-an-image

该文章指导你如何导出到Silverlight中的图像,但下载包含一些示例代码,告诉你如何在WPF中完成它。

我以前在我的应用程序中使用过这种技术,而且我总是发现使用该示例代码是最好的开始!

让我知道如果它不适合你 - 我在这方面有很多经验!我的提示是确保您在图表上正确的时间调用了Invalidate(),并确保它的布局在您尝试导出之前已经更新。

+0

令人惊讶的是,我没有阅读,因为我没有在Silverlight上。谢谢! – 2012-04-18 14:59:09

+0

很高兴能帮到你! – 2012-04-18 15:38:31

+0

无论如何,这并不能解决绑定数据的问题。因为我绑定看起来像绑定不处理,直到控件加载正确? – 2012-04-18 16:11:25