You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

后台生成Syncfusion图表无内容问题求助

问题

需要在后台生成包含Syncfusion SfChart和自定义图例的UserControl,并将其保存为PNG。当前代码中,UserControl的ActualWidth/Height已正常,自定义图例绑定显示正常,但SfChart始终为空无图形。已尝试UpdateLayout()和不同优先级的Dispatcher.BeginInvoke,均无效。

使用的代码如下:

private void GenerateGraphImage(object graphiqueBar, GraphiqueSynthese graphique)
{
    // binded property containing with some chart's datas
    GraphiqueSyntheseAAfficher = graphique;

    // try to force render user control
    if (graphiqueBar is UserControl userControl)
    {             
        userControl.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
        userControl.Arrange(new Rect(userControl.DesiredSize));
        userControl.UpdateLayout();

        // get image
        var image = ScreenshotHelper.GetImage(userControl);

        // save as png
        SaveImage(image, graphique.GraphiqueName);
    }
}

public static RenderTargetBitmap GetImage(UserControl view)
{
    Size size = new Size(view.ActualWidth, view.ActualHeight);
    if (size.IsEmpty)
        return null;

    RenderTargetBitmap result = new RenderTargetBitmap((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Pbgra32);

    DrawingVisual drawingvisual = new DrawingVisual();
    using (DrawingContext context = drawingvisual.RenderOpen())
    {
        context.DrawRectangle(new VisualBrush(view), null, new Rect(new Point(), size));
        context.Close();
    }

    result.Render(drawingvisual);
    return result;
}

解决方案与思路

1. 强制触发SfChart内部渲染逻辑

Syncfusion SfChart在后台渲染时,不会自动完成图表元素的绘制,需要手动触发其内部更新逻辑。可以调用图表的InvalidateMeasure()、InvalidateArrange(),或通过反射调用内部的UpdateChart方法:

if (userControl.FindName("YourChartName") is SfChart chart)
{
    chart.InvalidateMeasure();
    chart.InvalidateArrange();
    chart.UpdateLayout();
    // 若基础方法无效,尝试反射调用内部更新方法
    var updateChartMethod = typeof(SfChart).GetMethod("UpdateChart", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
    updateChartMethod?.Invoke(chart, null);
}

2. 修改截图方法,直接渲染UserControl

原截图方法通过VisualBrush转绘到DrawingVisual,可能无法捕获SfChart的复杂渲染内容。改为直接将UserControl渲染到RenderTargetBitmap:

public static RenderTargetBitmap GetImage(UserControl view)
{
    Size size = new Size(view.ActualWidth, view.ActualHeight);
    if (size.IsEmpty)
        return null;

    RenderTargetBitmap result = new RenderTargetBitmap((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Pbgra32);
    // 直接渲染目标控件
    result.Render(view);
    return result;
}

3. 临时将UserControl加入可视化树

WPF复杂控件(如SfChart)在未加入可视化树时,部分渲染逻辑不会执行。可以临时将UserControl添加到隐藏窗口中完成渲染:

if (graphiqueBar is UserControl userControl)
{
    // 创建临时隐藏窗口承载控件
    var tempWindow = new Window
    {
        Width = userControl.DesiredSize.Width,
        Height = userControl.DesiredSize.Height,
        WindowStyle = WindowStyle.None,
        ShowInTaskbar = false,
        Visibility = Visibility.Hidden
    };
    tempWindow.Content = userControl;
    tempWindow.Show();

    // 窗口加载完成后执行截图
    tempWindow.Loaded += (s, e) =>
    {
        userControl.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
        userControl.Arrange(new Rect(userControl.DesiredSize));
        userControl.UpdateLayout();

        // 额外触发图表渲染
        if (userControl.FindName("YourChartName") is SfChart chart)
        {
            chart.InvalidateVisual();
        }

        var image = ScreenshotHelper.GetImage(userControl);
        SaveImage(image, graphique.GraphiqueName);

        // 清理临时窗口
        tempWindow.Close();
    };
}

4. 等待数据绑定与渲染完成

使用Dispatcher的Render优先级等待UI线程完成所有绑定和渲染操作后再截图:

Dispatcher.Invoke(() =>
{
    userControl.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
    userControl.Arrange(new Rect(userControl.DesiredSize));
    userControl.UpdateLayout();

    if (userControl.FindName("YourChartName") is SfChart chart)
    {
        chart.InvalidateVisual();
    }

    var image = ScreenshotHelper.GetImage(userControl);
    SaveImage(image, graphique.GraphiqueName);
}, DispatcherPriority.Render);

内容的提问来源于stack exchange,提问作者Flo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 14:53:17