我已经在使用Windows表单图表,并且能够使用以下代码将图表控件保存在剪贴板中:
Dim stream As New System.IO.MemoryStream()
SummaryChart.SaveImage(stream, System.Drawing.Imaging.ImageFormat.Bmp)
Dim bmp As New System.Drawing.Bitmap(stream)
Clipboard.SetDataObject(bmp)
但是由于需求已更改,因此我不得不使用WPF图表工具包在视图中创建图表:
xmlns:dv="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
SummaryChart.SaveImage不再起作用。 做我以前所做的最好的方法是什么?
我已经阅读了这篇文章:Get a bitmap image from a Control view,并尝试了以下代码:
Dim rtb = New RenderTargetBitmap(CInt(SummariesChart.ActualWidth),
CInt(SummariesChart.ActualHeight), 96, 96, PixelFormats.Pbgra32)
rtb.Render(SummariesChart)
Dim png = New PngBitmapEncoder()
png.Frames.Add(BitmapFrame.Create(rtb))
Dim stream = New MemoryStream()
png.Save(stream)
Clipboard.SetImage(rtb)
但是,它仍然行不通。有人可以帮我吗?
谢谢。
答案 0 :(得分:0)
要保存WPF图表工具包图像,如以下代码所示。在Encoder.Frames中添加BitMapFrame,然后保存图像。
Public Shared Sub SaveAsImage(ByVal element As FrameworkElement, ByVal filepath As String, ByVal width As Integer, ByVal height As Integer)
element.Width = width
element.Height = height
element.Measure(New Size(width, height))
element.Arrange(New Rect(0, 0, width, height))
element.UpdateLayout()
Dim target = New RenderTargetBitmap(width, height, 96, 96, System.Windows.Media.PixelFormats.Pbgra32)
target.Render(element)
Dim encoder = New PngBitmapEncoder()
Dim outputFrame = BitmapFrame.Create(target)
encoder.Frames.Add(outputFrame)
Using file = File.OpenWrite(filepath)
encoder.Save(file)
End Using End Sub