Skip to content

Commit 1816e91

Browse files
Merge pull request #1 from SyncfusionExamples/Added-sample-demo
How to export chart as image sample demo added
2 parents baa9267 + 87e35c1 commit 1816e91

21 files changed

+539
-2
lines changed

README.md

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,88 @@
1-
# How-to-Export-Chart-as-Image-in-WinUI-Chart
2-
How to Export Chart as Image in WinUI Chart
1+
# How to Export Chart as Image in WinUI Chart
2+
This article provides a detailed walkthrough on how to export a [WinUI Cartesian Chart](https://www.syncfusion.com/winui-controls/cartesian-charts) as an image. You can export the chart view in your desired file format, with supported formats being **JPEG** and **PNG.**
3+
4+
### Initialize SfCartesianChart:
5+
6+
Set up the **SfCartesianChart** following the [ Syncfusion WinUI Cartesian Chart documentation.](https://help.syncfusion.com/winui/cartesian-charts/getting-started)
7+
```xml
8+
<StackPanel Orientation="Vertical">
9+
<chart:SfCartesianChart x:Name="Chart" Background="White" IsTransposed="True"
10+
Header="Daily Water Consumption Tracking">
11+
....
12+
<chart:ColumnSeries ItemsSource="{Binding DailyWaterIntake}"
13+
XBindingPath="Day"
14+
YBindingPath="Liters"
15+
ShowDataLabels="True">
16+
<chart:ColumnSeries.DataLabelSettings>
17+
<chart:CartesianDataLabelSettings Position="Inner"/>
18+
</chart:ColumnSeries.DataLabelSettings>
19+
</chart:ColumnSeries>
20+
</chart:SfCartesianChart>
21+
22+
<Button x:Name="button" Content="Export as Image" Click="Button_Click" />
23+
</StackPanel>
24+
```
25+
26+
### Export the Chart as an Image:
27+
28+
```csharp
29+
private async void Button_Click(object sender, RoutedEventArgs e)
30+
{
31+
await SaveAsImageAsync(Chart, "chart.png");
32+
}
33+
34+
private async Task SaveAsImageAsync(SfCartesianChart chart, string fileName)
35+
{
36+
if (chart == null)
37+
return;
38+
39+
// Render the chart to a RenderTargetBitmap
40+
var renderTargetBitmap = new RenderTargetBitmap();
41+
await renderTargetBitmap.RenderAsync(chart);
42+
43+
// Get pixel data
44+
var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();
45+
var pixels = pixelBuffer.ToArray();
46+
47+
// Determine file format and encoder
48+
string extension = Path.GetExtension(fileName)?.ToLower() ?? ".png";
49+
var encoderId = extension == ".jpg" || extension == ".jpeg"
50+
? BitmapEncoder.JpegEncoderId
51+
: BitmapEncoder.PngEncoderId;
52+
53+
// Choose image save path
54+
var folder = await Windows.Storage.StorageFolder.GetFolderFromPathAsync(@"D:\");
55+
var picturesFolder = await folder.CreateFileAsync( fileName, Windows.Storage.CreationCollisionOption.ReplaceExisting);
56+
57+
// Encode the image
58+
using (var stream = await picturesFolder.OpenAsync(FileAccessMode.ReadWrite))
59+
{
60+
var encoder = await BitmapEncoder.CreateAsync(encoderId, stream);
61+
62+
encoder.SetPixelData(
63+
BitmapPixelFormat.Bgra8,
64+
BitmapAlphaMode.Premultiplied,
65+
(uint)renderTargetBitmap.PixelWidth,
66+
(uint)renderTargetBitmap.PixelHeight,
67+
96.0, // DPI X
68+
96.0, // DPI Y
69+
pixels);
70+
71+
await encoder.FlushAsync();
72+
}
73+
}
74+
```
75+
76+
**Note:** By default, the chart background is transparent. When using JPEG format, RenderTargetBitmap converts the transparent background to black. To resolve this, set the chart’s BackgroundColor to white or any preferred color.
77+
78+
**Chart output**
79+
80+
![Chart-WinUI.png](https://support.syncfusion.com/kb/agent/attachment/article/18644/inline?token=eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjM0MjM0Iiwib3JnaWQiOiIzIiwiaXNzIjoic3VwcG9ydC5zeW5jZnVzaW9uLmNvbSJ9.0wMM_ZaV1z0RXFn8efmG0bKwTOt6QMiZ7DQ5fa1QLyU)
81+
82+
**Exported Chart Image**
83+
84+
![chart.png](https://support.syncfusion.com/kb/agent/attachment/article/18644/inline?token=eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjM0MjMyIiwib3JnaWQiOiIzIiwiaXNzIjoic3VwcG9ydC5zeW5jZnVzaW9uLmNvbSJ9.VhSO304zS7VSvBDSySJWOBxdySgRK0LWuAdmx3Vl4No)
85+
86+
### KB link
87+
For a more detailed, refer to the [Export Chart View as Image KB.](https://support.syncfusion.com/agent/kb/18644)
88+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35506.116 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinUISampleDemo", "WinUISampleDemo\WinUISampleDemo.csproj", "{0C10A7EF-1D8E-48E0-A15A-D865C3FA5F58}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|ARM64 = Debug|ARM64
11+
Debug|x64 = Debug|x64
12+
Debug|x86 = Debug|x86
13+
Release|ARM64 = Release|ARM64
14+
Release|x64 = Release|x64
15+
Release|x86 = Release|x86
16+
EndGlobalSection
17+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
18+
{0C10A7EF-1D8E-48E0-A15A-D865C3FA5F58}.Debug|ARM64.ActiveCfg = Debug|ARM64
19+
{0C10A7EF-1D8E-48E0-A15A-D865C3FA5F58}.Debug|ARM64.Build.0 = Debug|ARM64
20+
{0C10A7EF-1D8E-48E0-A15A-D865C3FA5F58}.Debug|ARM64.Deploy.0 = Debug|ARM64
21+
{0C10A7EF-1D8E-48E0-A15A-D865C3FA5F58}.Debug|x64.ActiveCfg = Debug|x64
22+
{0C10A7EF-1D8E-48E0-A15A-D865C3FA5F58}.Debug|x64.Build.0 = Debug|x64
23+
{0C10A7EF-1D8E-48E0-A15A-D865C3FA5F58}.Debug|x64.Deploy.0 = Debug|x64
24+
{0C10A7EF-1D8E-48E0-A15A-D865C3FA5F58}.Debug|x86.ActiveCfg = Debug|x86
25+
{0C10A7EF-1D8E-48E0-A15A-D865C3FA5F58}.Debug|x86.Build.0 = Debug|x86
26+
{0C10A7EF-1D8E-48E0-A15A-D865C3FA5F58}.Debug|x86.Deploy.0 = Debug|x86
27+
{0C10A7EF-1D8E-48E0-A15A-D865C3FA5F58}.Release|ARM64.ActiveCfg = Release|ARM64
28+
{0C10A7EF-1D8E-48E0-A15A-D865C3FA5F58}.Release|ARM64.Build.0 = Release|ARM64
29+
{0C10A7EF-1D8E-48E0-A15A-D865C3FA5F58}.Release|ARM64.Deploy.0 = Release|ARM64
30+
{0C10A7EF-1D8E-48E0-A15A-D865C3FA5F58}.Release|x64.ActiveCfg = Release|x64
31+
{0C10A7EF-1D8E-48E0-A15A-D865C3FA5F58}.Release|x64.Build.0 = Release|x64
32+
{0C10A7EF-1D8E-48E0-A15A-D865C3FA5F58}.Release|x64.Deploy.0 = Release|x64
33+
{0C10A7EF-1D8E-48E0-A15A-D865C3FA5F58}.Release|x86.ActiveCfg = Release|x86
34+
{0C10A7EF-1D8E-48E0-A15A-D865C3FA5F58}.Release|x86.Build.0 = Release|x86
35+
{0C10A7EF-1D8E-48E0-A15A-D865C3FA5F58}.Release|x86.Deploy.0 = Release|x86
36+
EndGlobalSection
37+
GlobalSection(SolutionProperties) = preSolution
38+
HideSolutionNode = FALSE
39+
EndGlobalSection
40+
EndGlobal
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Application
3+
x:Class="WinUISampleDemo.App"
4+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
6+
xmlns:local="using:WinUISampleDemo">
7+
<Application.Resources>
8+
<ResourceDictionary>
9+
<ResourceDictionary.MergedDictionaries>
10+
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
11+
<!-- Other merged dictionaries here -->
12+
</ResourceDictionary.MergedDictionaries>
13+
<!-- Other app resources here -->
14+
</ResourceDictionary>
15+
</Application.Resources>
16+
</Application>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Runtime.InteropServices.WindowsRuntime;
6+
using Microsoft.UI.Xaml;
7+
using Microsoft.UI.Xaml.Controls;
8+
using Microsoft.UI.Xaml.Controls.Primitives;
9+
using Microsoft.UI.Xaml.Data;
10+
using Microsoft.UI.Xaml.Input;
11+
using Microsoft.UI.Xaml.Media;
12+
using Microsoft.UI.Xaml.Navigation;
13+
using Microsoft.UI.Xaml.Shapes;
14+
using Windows.ApplicationModel;
15+
using Windows.ApplicationModel.Activation;
16+
using Windows.Foundation;
17+
using Windows.Foundation.Collections;
18+
19+
// To learn more about WinUI, the WinUI project structure,
20+
// and more about our project templates, see: http://aka.ms/winui-project-info.
21+
22+
namespace WinUISampleDemo
23+
{
24+
/// <summary>
25+
/// Provides application-specific behavior to supplement the default Application class.
26+
/// </summary>
27+
public partial class App : Application
28+
{
29+
/// <summary>
30+
/// Initializes the singleton application object. This is the first line of authored code
31+
/// executed, and as such is the logical equivalent of main() or WinMain().
32+
/// </summary>
33+
public App()
34+
{
35+
this.InitializeComponent();
36+
}
37+
38+
/// <summary>
39+
/// Invoked when the application is launched.
40+
/// </summary>
41+
/// <param name="args">Details about the launch request and process.</param>
42+
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
43+
{
44+
m_window = new MainWindow();
45+
m_window.Activate();
46+
}
47+
48+
private Window? m_window;
49+
}
50+
}
432 Bytes
Loading
5.25 KB
Loading
1.71 KB
Loading
637 Bytes
Loading
283 Bytes
Loading
456 Bytes
Loading

0 commit comments

Comments
 (0)