【问题标题】:Creating Images in WPF without XAML and dispaying在没有 XAML 的 WPF 中创建图像并显示
【发布时间】:2010-09-07 09:21:27
【问题描述】:

我正在尝试在 c# 代码中创建一个 WPF 图表并将其保存到文件而不显示在屏幕上。这将在 WCF 服务中,其中数据被发送到服务,创建图像并返回图像的路径。

到目前为止,我已经将图像保存到文件中,并且显示了 X 和 Y 轴上的数据,但未绘制图表上的列。

有谁知道为什么图表上的列没有绘制...这是我的代码:

公共无效 DoWork() { var newThread = new Thread(CreateImage); newThread.SetApartmentState(ApartmentState.STA); newThread.Start(); }

    public void CreateImage()
    {
        var grid = new Grid {Width = 800, Height = 600, Background = new SolidColorBrush(Colors.LightBlue)};
        var chart = new Chart();
        grid.Children.Add(chart);

        var renderTarget = new RenderTargetBitmap((int)grid.Width, (int)grid.Height, 96d, 96d, PixelFormats.Default);


        var barSeries = new BarSeries
                            {
                                Title = "Fruit in Cupboard",
                                ItemsSource = new[]
                                                  {
                                                      new KeyValuePair<string, int>("Oranges", 18),
                                                      new KeyValuePair<string, int>("Apples", 15),
                                                      new KeyValuePair<string, int>("Melons", 2),
                                                      new KeyValuePair<string, int>("Pineapples", 4),
                                                      new KeyValuePair<string, int>("Plums", 25)
                                                  },
                                IndependentValueBinding = new Binding("Key"),
                                DependentValueBinding = new Binding("Value")
                            };

        chart.Series.Add(barSeries);

        grid.Measure(new Size(grid.Width, grid.Height));
        grid.Arrange(new Rect(new Size(grid.Width, grid.Height)));
        grid.UpdateLayout();

        renderTarget.Render(grid);

        var png = new PngBitmapEncoder();
        png.Frames.Add(BitmapFrame.Create(renderTarget));
        using (Stream stm = File.Create(string.Format(@"C:/Images/{0}.png", Guid.NewGuid())))
        {
            png.Save(stm);
        }
    }

【问题讨论】:

  • 我认为这与以下事实有关动画发生前图表的快照。

标签: wpf charts wpftoolkit


【解决方案1】:

我在 Telerik 的 RadChart 中遇到了这个问题。解释正如您在上面的评论中所写的那样 - 该系列正在制作动画,并且在它们可见之前拍摄了快照。

我的解决方案是在渲染之前将 ChartArea.EnableAnimations 属性的值设置为 false。我不熟悉 WPF 工具包图表,但很可能有一个类似的设置可用于禁用动画。

请注意,Telerik 的 BarSeries 存在另一个相关问题 - 必须将条形的不透明度设置为 1。有关详细信息,请参阅 Telerik documentation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-13
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-23
    • 2019-06-05
    • 2011-01-20
    相关资源
    最近更新 更多