在 WPF 中创建图像而不使用 XAML 并显示

发布于 2024-09-17 11:58:19 字数 2199 浏览 2 评论 0原文

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

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

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

public void DoWork() { var newThread = 新线程(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);
        }
    }

I am trying to create a WPF Chart in c# code and saving it to file without displaying it on screen. This will be in a WCF Service where data is sent to the service, an image is created and and path to the image is returned.

So far I have got the image to save to file and the data on the X & Y axis is displayed but the columns on the graph are not drawn.

Does anyone know why the columns on the chart are not drawing... here is my code:

public void 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);
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

为人所爱 2024-09-24 11:58:38

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

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

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

I had exactly this problem with Telerik's RadChart. The explanation was as you wrote in your comment above - the series was being animated in and the snapshot was being taken before they were visible.

My solution was to set the value of the ChartArea.EnableAnimations property to false before rendering it. I am not familiar with the WPF Toolkit Chart but it is likely that there is a similar setting that you can use to disable the animations.

Note that there is a further, related issue with Telerik's BarSeries - it is necessary to set the opacity of the bars to 1. See the Telerik documentation for details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文