我正在尝试执行以下操作:
- 在内存中创建 Livechart 笛卡尔图表
- 将图表添加到网格
- 将标签添加到同一网格
- 将网格添加到 Viewbox
- 将 Viewbox 渲染为 PNG
- 将 PNG 保存到磁盘
上面应该从后台的不同线程运行,以允许 UI 响应。
不管这看起来多么简单,我一直在努力寻找一个合适的工作解决方案。以下问题是相关的:
- Livechart(位于 Viewbox 内)需要时间来渲染
- 因此,在尝试将图表保存为图像之前,需要给图表时间来完成渲染
- 我找到了使用 HwndSource 的代码,但它并非一直有效(大约 95% 的时间有效)。没有 HwndSource 修改它永远不会工作(总是得到一个没有任何内容的图表)
- 在不同的 UI 线程中运行 Run() 函数不起作用,因为我收到以下错误消息: WPF Dispatcher {“调用线程无法访问此对象,因为不同的线程拥有它。”}
所以我的问题是:
- 在将 Livechart/Grid/ViewBox 组合保存为图像之前等待其完成渲染的正确方法是什么?也许利用 Loaded 事件?请注意,我试图推动它,但在遇到“线程”问题时无法使其工作。
- 如何在不同的 UI 线程中运行整个过程?
请参阅下面的代码
public void Run()
(
//Create Livechart which is a child of a Grid control
Grid gridChart = Charts.CreateChart();
//Creates a ViewBox control which has the grid as its child
Viewbox viewBox = WrapChart(gridChart,1400,700);
//Creates and saves the image
CreateAndSaveImage(viewBox ,path,name);
)
下面是创建 Viewbox 并将网格添加为子项的函数
public Viewbox viewBox WrapChart(Grid grid,int width,int height)
{
chart.grid.Width = width;
chart.grid.Height = height;
viewbox.Child = chart.grid;
viewbox.Width = width;
viewbox.Height = height;
viewbox.Measure(new System.Windows.Size(width, height));
viewbox.Arrange(new Rect(0, 0, width, height));
viewbox.UpdateLayout();
}
下面的函数创建并保存图像
public void CreateAndSaveImage(Viewbox viewbox,string folderPath,string fileName)
{
var x = HelperFunctions.GetImage(viewbox);
System.IO.FileStream stream = System.IO.File.Create(folderPath + fileName);
HelperFunctions.SaveAsPng(x, stream);
stream.Close();
}
以下代码将视图框呈现为图像。请注意,这是我能找到的唯一等待图表完成加载的代码。我不知道它是如何工作的,但它在 95% 的时间里都有效。有时图表仍未完成加载。
public static RenderTargetBitmap GetImage(Viewbox view)
{
using (new HwndSource(new HwndSourceParameters())
{
RootVisual =
(VisualTreeHelper.GetParent(view) == null
? view
: null)
})
{
Size size = new Size(view.ActualWidth, view.ActualHeight);
if (size.IsEmpty)
return null;
int actualWidth = Convert.ToInt32(size.Width);
int requiredWidth = Convert.ToInt32(size.Width * 1);
int actualHeight = Convert.ToInt32(size.Height);
int requiredHeight = Convert.ToInt32(size.Height * 1);
// Flush the dispatcher queue
view.Dispatcher.Invoke(DispatcherPriority.SystemIdle, new Action(() => { }));
var renderBitmap = new RenderTargetBitmap(requiredWidth, requiredHeight,
96d * requiredWidth / actualWidth, 96d * requiredHeight / actualHeight,
PixelFormats.Pbgra32);
DrawingVisual drawingvisual = new DrawingVisual();
using (DrawingContext context = drawingvisual.RenderOpen())
{
context.DrawRectangle(new VisualBrush(view), null, new Rect(new Point(), size));
context.Close();
}
renderBitmap.Render(view);
renderBitmap.Freeze();
return renderBitmap;
}
}
以下代码将位图保存为图片到文件
public static void SaveAsPng(BitmapSource src, Stream outputStream)
{
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(src));
encoder.Save(outputStream);
}
下面的代码是我用来在不同的线程中运行整个事情的代码。请注意,它不起作用,因为我收到以下错误消息:
WPF Dispatcher {“调用线程无法访问此对象,因为不同的线程拥有它。”}。
请注意,如果我正常执行 Run()(没有任何单独的线程)它可以工作,但有时图表无法正确呈现(如前所述)。
Thread thread = new Thread(() =>
{
Run();
System.Windows.Threading.Dispatcher.Run();
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();