Added a way to disable raster canvas scaling for Windows controls

This commit is contained in:
Matthew Leibowitz 2016-11-12 23:34:05 +02:00
Родитель c3845b1613
Коммит c306030934
3 изменённых файлов: 75 добавлений и 10 удалений

Просмотреть файл

@ -1,4 +1,5 @@
using System;
using System.ComponentModel;
using Xamarin.Forms;
using Xamarin.Forms.Platform.UWP;
@ -28,6 +29,7 @@ namespace SkiaSharp.Views.Forms
// create the native view
var view = new InternalView(newController);
view.IgnorePixelScaling = e.NewElement.IgnorePixelScaling;
SetNativeControl(view);
// subscribe to events from the user
@ -41,6 +43,16 @@ namespace SkiaSharp.Views.Forms
base.OnElementChanged(e);
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == nameof(SKFormsView.IgnorePixelScaling))
{
Control.IgnorePixelScaling = Element.IgnorePixelScaling;
}
}
protected override void Dispose(bool disposing)
{
// detach all events before disposing

Просмотреть файл

@ -19,6 +19,7 @@ namespace SkiaSharp.Views.UWP
private GCHandle buff;
private WriteableBitmap bitmap;
private double dpi;
private bool ignorePixelScaling;
public SKXamlCanvas()
{
@ -41,6 +42,16 @@ namespace SkiaSharp.Views.UWP
public SKSize CanvasSize => bitmap == null ? SKSize.Empty : new SKSize(bitmap.PixelWidth, bitmap.PixelHeight);
public bool IgnorePixelScaling
{
get { return ignorePixelScaling; }
set
{
ignorePixelScaling = value;
Invalidate();
}
}
public event EventHandler<SKPaintSurfaceEventArgs> PaintSurface;
protected virtual void OnPaintSurface(SKPaintSurfaceEventArgs e)
@ -72,7 +83,19 @@ namespace SkiaSharp.Views.UWP
if (ActualWidth == 0 || ActualHeight == 0 || Visibility != Visibility.Visible)
return;
var info = new SKImageInfo((int)(ActualWidth * dpi), (int)(ActualHeight * dpi), SKImageInfo.PlatformColorType, SKAlphaType.Premul);
int width, height;
if (IgnorePixelScaling)
{
width = (int)ActualWidth;
height = (int)ActualHeight;
}
else
{
width = (int)(ActualWidth * dpi);
height = (int)(ActualHeight * dpi);
}
var info = new SKImageInfo(width, height, SKImageInfo.PlatformColorType, SKAlphaType.Premul);
CreateBitmap(info);
using (var surface = SKSurface.Create(info, buff.AddrOfPinnedObject(), info.RowBytes))
{
@ -101,15 +124,23 @@ namespace SkiaSharp.Views.UWP
}
bitmap = new WriteableBitmap(info.Width, info.Height);
var scale = 1.0 / dpi;
Background = new ImageBrush
var brush = new ImageBrush
{
ImageSource = bitmap,
AlignmentX = AlignmentX.Left,
AlignmentY = AlignmentY.Top,
Stretch = Stretch.None,
Transform = new ScaleTransform { ScaleX = scale, ScaleY = scale }
Stretch = Stretch.None
};
if (!IgnorePixelScaling)
{
var scale = 1.0 / dpi;
brush.Transform = new ScaleTransform
{
ScaleX = scale,
ScaleY = scale
};
}
Background = brush;
}
}

Просмотреть файл

@ -16,6 +16,7 @@ namespace SkiaSharp.Views.WPF
private readonly bool designMode;
private WriteableBitmap bitmap;
private bool ignorePixelScaling;
public SKElement()
{
@ -24,6 +25,16 @@ namespace SkiaSharp.Views.WPF
public SKSize CanvasSize => bitmap == null ? SKSize.Empty : new SKSize(bitmap.PixelWidth, bitmap.PixelHeight);
public bool IgnorePixelScaling
{
get { return ignorePixelScaling; }
set
{
ignorePixelScaling = value;
InvalidateVisual();
}
}
public event EventHandler<SKPaintSurfaceEventArgs> PaintSurface;
protected override void OnRender(DrawingContext drawingContext)
@ -33,11 +44,22 @@ namespace SkiaSharp.Views.WPF
base.OnRender(drawingContext);
var m = PresentationSource.FromVisual(this).CompositionTarget.TransformToDevice;
var dpiX = m.M11;
var dpiY = m.M22;
var width = (int)(ActualWidth * dpiX);
var height = (int)(ActualHeight * dpiY);
int width, height;
double dpiX = 1.0;
double dpiY = 1.0;
if (IgnorePixelScaling)
{
width = (int)ActualWidth;
height = (int)ActualHeight;
}
else
{
var m = PresentationSource.FromVisual(this).CompositionTarget.TransformToDevice;
dpiX = m.M11;
dpiY = m.M22;
width = (int)(ActualWidth * dpiX);
height = (int)(ActualHeight * dpiY);
}
var info = new SKImageInfo(width, height, SKImageInfo.PlatformColorType, SKAlphaType.Premul);