mirror of
https://gitee.com/akwkevin/aistudio.-wpf.-diagram
synced 2026-04-05 00:37:19 +08:00
项目结构调整
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
<UserControl x:Class="Wpf.CartesianChart.Chart_to_Image.ChartToImageSample"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="clr-namespace:Wpf.CartesianChart.Chart_to_Image"
|
||||
mc:Ignorable="d"
|
||||
d:DesignHeight="300" d:DesignWidth="300">
|
||||
<Grid>
|
||||
<Button Click="BuildPngOnClick">Build Png</Button>
|
||||
</Grid>
|
||||
</UserControl>
|
||||
@@ -0,0 +1,63 @@
|
||||
using System.IO;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using LiveCharts;
|
||||
using LiveCharts.Wpf;
|
||||
|
||||
namespace Wpf.CartesianChart.Chart_to_Image
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for ChartToImageSample.xaml
|
||||
/// </summary>
|
||||
public partial class ChartToImageSample : UserControl
|
||||
{
|
||||
public ChartToImageSample()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void BuildPngOnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var myChart = new LiveCharts.Wpf.CartesianChart
|
||||
{
|
||||
DisableAnimations = true,
|
||||
Width = 600,
|
||||
Height = 200,
|
||||
Series = new SeriesCollection
|
||||
{
|
||||
new LineSeries
|
||||
{
|
||||
Values = new ChartValues<double> {1, 6, 7, 2, 9, 3, 6, 5}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var viewbox = new Viewbox();
|
||||
viewbox.Child = myChart;
|
||||
viewbox.Measure(myChart.RenderSize);
|
||||
viewbox.Arrange(new Rect(new Point(0, 0), myChart.RenderSize));
|
||||
myChart.Update(true, true); //force chart redraw
|
||||
viewbox.UpdateLayout();
|
||||
|
||||
SaveToPng(myChart, "chart.png");
|
||||
//png file was created at the root directory.
|
||||
}
|
||||
|
||||
private void SaveToPng(FrameworkElement visual, string fileName)
|
||||
{
|
||||
var encoder = new PngBitmapEncoder();
|
||||
EncodeVisual(visual, fileName, encoder);
|
||||
}
|
||||
|
||||
private static void EncodeVisual(FrameworkElement visual, string fileName, BitmapEncoder encoder)
|
||||
{
|
||||
var bitmap = new RenderTargetBitmap((int)visual.ActualWidth, (int)visual.ActualHeight, 96, 96, PixelFormats.Pbgra32);
|
||||
bitmap.Render(visual);
|
||||
var frame = BitmapFrame.Create(bitmap);
|
||||
encoder.Frames.Add(frame);
|
||||
using (var stream = File.Create(fileName)) encoder.Save(stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user