Added a way to disable raster canvas scaling for Windows controls
This commit is contained in:
Родитель
c3845b1613
Коммит
c306030934
|
@ -1,4 +1,5 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.ComponentModel;
|
||||||
using Xamarin.Forms;
|
using Xamarin.Forms;
|
||||||
using Xamarin.Forms.Platform.UWP;
|
using Xamarin.Forms.Platform.UWP;
|
||||||
|
|
||||||
|
@ -28,6 +29,7 @@ namespace SkiaSharp.Views.Forms
|
||||||
|
|
||||||
// create the native view
|
// create the native view
|
||||||
var view = new InternalView(newController);
|
var view = new InternalView(newController);
|
||||||
|
view.IgnorePixelScaling = e.NewElement.IgnorePixelScaling;
|
||||||
SetNativeControl(view);
|
SetNativeControl(view);
|
||||||
|
|
||||||
// subscribe to events from the user
|
// subscribe to events from the user
|
||||||
|
@ -41,6 +43,16 @@ namespace SkiaSharp.Views.Forms
|
||||||
base.OnElementChanged(e);
|
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)
|
protected override void Dispose(bool disposing)
|
||||||
{
|
{
|
||||||
// detach all events before disposing
|
// detach all events before disposing
|
||||||
|
|
|
@ -19,6 +19,7 @@ namespace SkiaSharp.Views.UWP
|
||||||
private GCHandle buff;
|
private GCHandle buff;
|
||||||
private WriteableBitmap bitmap;
|
private WriteableBitmap bitmap;
|
||||||
private double dpi;
|
private double dpi;
|
||||||
|
private bool ignorePixelScaling;
|
||||||
|
|
||||||
public SKXamlCanvas()
|
public SKXamlCanvas()
|
||||||
{
|
{
|
||||||
|
@ -41,6 +42,16 @@ namespace SkiaSharp.Views.UWP
|
||||||
|
|
||||||
public SKSize CanvasSize => bitmap == null ? SKSize.Empty : new SKSize(bitmap.PixelWidth, bitmap.PixelHeight);
|
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;
|
public event EventHandler<SKPaintSurfaceEventArgs> PaintSurface;
|
||||||
|
|
||||||
protected virtual void OnPaintSurface(SKPaintSurfaceEventArgs e)
|
protected virtual void OnPaintSurface(SKPaintSurfaceEventArgs e)
|
||||||
|
@ -72,7 +83,19 @@ namespace SkiaSharp.Views.UWP
|
||||||
if (ActualWidth == 0 || ActualHeight == 0 || Visibility != Visibility.Visible)
|
if (ActualWidth == 0 || ActualHeight == 0 || Visibility != Visibility.Visible)
|
||||||
return;
|
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);
|
CreateBitmap(info);
|
||||||
using (var surface = SKSurface.Create(info, buff.AddrOfPinnedObject(), info.RowBytes))
|
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);
|
bitmap = new WriteableBitmap(info.Width, info.Height);
|
||||||
|
|
||||||
var scale = 1.0 / dpi;
|
var brush = new ImageBrush
|
||||||
Background = new ImageBrush
|
|
||||||
{
|
{
|
||||||
ImageSource = bitmap,
|
ImageSource = bitmap,
|
||||||
AlignmentX = AlignmentX.Left,
|
AlignmentX = AlignmentX.Left,
|
||||||
AlignmentY = AlignmentY.Top,
|
AlignmentY = AlignmentY.Top,
|
||||||
Stretch = Stretch.None,
|
Stretch = Stretch.None
|
||||||
Transform = new ScaleTransform { ScaleX = scale, ScaleY = scale }
|
|
||||||
};
|
};
|
||||||
|
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 readonly bool designMode;
|
||||||
|
|
||||||
private WriteableBitmap bitmap;
|
private WriteableBitmap bitmap;
|
||||||
|
private bool ignorePixelScaling;
|
||||||
|
|
||||||
public SKElement()
|
public SKElement()
|
||||||
{
|
{
|
||||||
|
@ -24,6 +25,16 @@ namespace SkiaSharp.Views.WPF
|
||||||
|
|
||||||
public SKSize CanvasSize => bitmap == null ? SKSize.Empty : new SKSize(bitmap.PixelWidth, bitmap.PixelHeight);
|
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;
|
public event EventHandler<SKPaintSurfaceEventArgs> PaintSurface;
|
||||||
|
|
||||||
protected override void OnRender(DrawingContext drawingContext)
|
protected override void OnRender(DrawingContext drawingContext)
|
||||||
|
@ -33,11 +44,22 @@ namespace SkiaSharp.Views.WPF
|
||||||
|
|
||||||
base.OnRender(drawingContext);
|
base.OnRender(drawingContext);
|
||||||
|
|
||||||
|
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;
|
var m = PresentationSource.FromVisual(this).CompositionTarget.TransformToDevice;
|
||||||
var dpiX = m.M11;
|
dpiX = m.M11;
|
||||||
var dpiY = m.M22;
|
dpiY = m.M22;
|
||||||
var width = (int)(ActualWidth * dpiX);
|
width = (int)(ActualWidth * dpiX);
|
||||||
var height = (int)(ActualHeight * dpiY);
|
height = (int)(ActualHeight * dpiY);
|
||||||
|
}
|
||||||
|
|
||||||
var info = new SKImageInfo(width, height, SKImageInfo.PlatformColorType, SKAlphaType.Premul);
|
var info = new SKImageInfo(width, height, SKImageInfo.PlatformColorType, SKAlphaType.Premul);
|
||||||
|
|
||||||
|
|
Загрузка…
Ссылка в новой задаче