Created
December 16, 2020 12:48
-
-
Save mterwoord/ee7235c1b16e3034d264733f8b6cfec1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using Windows.UI.Xaml; | |
using Windows.UI.Xaml.Controls; | |
using Windows.UI.Xaml.Media; | |
using Windows.UI.Xaml.Media.Imaging; | |
using SkiaSharp; | |
namespace Microcharts.Uwp | |
{ | |
[TemplatePart(Name = ImagePart, Type = typeof(Image))] | |
public partial class SecondChartView: Control | |
{ | |
public const string ImagePart = "PART_Image"; | |
public SecondChartView() | |
{ | |
this.DefaultStyleKey = typeof(SecondChartView); | |
SizeChanged += OnSizeChanged; | |
} | |
#region Properties | |
#region Chart property | |
public static readonly DependencyProperty ChartProperty = DependencyProperty.Register(nameof(Chart), | |
typeof(Chart), | |
typeof(SecondChartView), | |
new PropertyMetadata(null, ChartPropertyChanged)); | |
public Chart Chart | |
{ | |
get => GetValue(ChartProperty) as Chart; | |
set => SetValue(ChartProperty, value); | |
} | |
private static void ChartPropertyChanged(object sender, DependencyPropertyChangedEventArgs eventArgs) | |
{ | |
((SecondChartView)sender).ChartPropertyChanged(); | |
} | |
private void ChartPropertyChanged() | |
{ | |
GenerateSource(); | |
} | |
#endregion Chart property | |
public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.Register(nameof(ImageSource), | |
typeof(ImageSource), | |
typeof(SecondChartView), | |
new PropertyMetadata(null)); | |
public ImageSource ImageSource | |
{ | |
get => GetValue(ImageSourceProperty) as ImageSource; | |
private set => SetValue(ImageSourceProperty, value); | |
} | |
#endregion Properties | |
private void GenerateSource() | |
{ | |
if (Chart == null) | |
{ | |
ImageSource = null; | |
return; | |
} | |
var actualWidth = (int)ActualWidth; | |
var actualHeight = (int)ActualHeight; | |
if (actualHeight <= 0 | |
|| actualWidth <= 0) | |
{ | |
ImageSource = null; | |
Console.WriteLine("SKipped, no size"); | |
return; | |
} | |
Console.WriteLine("Generated {0}/{1}", actualWidth, actualHeight); | |
ApplyTemplate(); | |
// apply render tweaks: | |
Chart.IsAnimated = false; | |
// end apply render tweaks | |
MemoryStream ms; | |
using (var bitmap = new SKBitmap(actualWidth, actualHeight)) | |
{ | |
using (var canvas = new SKCanvas(bitmap)) | |
{ | |
Chart.Draw(canvas, actualWidth, actualHeight); | |
canvas.Flush(); | |
} | |
ms = new MemoryStream(); | |
bitmap.Encode(ms, SKEncodedImageFormat.Png, 100); | |
} | |
ms.Position = 0; | |
var result = new BitmapImage(); | |
result.SetSource(ms.AsRandomAccessStream()); | |
_partImage.Source = result; | |
Console.WriteLine("Source set"); | |
} | |
private Image _partImage; | |
protected override void OnApplyTemplate() | |
{ | |
base.OnApplyTemplate(); | |
_partImage = (Image)GetTemplateChild(ImagePart); | |
} | |
private void OnSizeChanged(object sender, SizeChangedEventArgs args) | |
{ | |
GenerateSource(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Style TargetType="uwp1:SecondChartView"> | |
<Setter Property="Template"> | |
<Setter.Value> | |
<ControlTemplate> | |
<Image x:Name="PART_Image" /> | |
</ControlTemplate> | |
</Setter.Value> | |
</Setter> | |
</Style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment