|
|
using System; |
|
|
namespace SkiaSharp.Views.Desktop |
|
|
{ |
|
|
public static class Extensions |
|
|
{ |
|
|
private static readonly Lazy<bool> isValidEnvironment = new Lazy<bool>(() => |
|
|
{ |
|
|
try |
|
|
{ |
|
|
|
|
|
SKPMColor.PreMultiply(SKColors.Black); |
|
|
return true; |
|
|
} |
|
|
catch (DllNotFoundException) |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return false; |
|
|
} |
|
|
}); |
|
|
|
|
|
internal static bool IsValidEnvironment => isValidEnvironment.Value; |
|
|
|
|
|
#if !WINDOWS_UWP |
|
|
|
|
|
|
|
|
public static SKPoint ToSKPoint(this System.Drawing.PointF point) |
|
|
{ |
|
|
return new SKPoint(point.X, point.Y); |
|
|
} |
|
|
|
|
|
public static SKPointI ToSKPoint(this System.Drawing.Point point) |
|
|
{ |
|
|
return new SKPointI(point.X, point.Y); |
|
|
} |
|
|
|
|
|
public static System.Drawing.PointF ToDrawingPoint(this SKPoint point) |
|
|
{ |
|
|
return new System.Drawing.PointF(point.X, point.Y); |
|
|
} |
|
|
|
|
|
public static System.Drawing.Point ToDrawingPoint(this SKPointI point) |
|
|
{ |
|
|
return new System.Drawing.Point(point.X, point.Y); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static SKRect ToSKRect(this System.Drawing.RectangleF rect) |
|
|
{ |
|
|
return new SKRect(rect.Left, rect.Top, rect.Right, rect.Bottom); |
|
|
} |
|
|
|
|
|
public static SKRectI ToSKRect(this System.Drawing.Rectangle rect) |
|
|
{ |
|
|
return new SKRectI(rect.Left, rect.Top, rect.Right, rect.Bottom); |
|
|
} |
|
|
|
|
|
public static System.Drawing.RectangleF ToDrawingRect(this SKRect rect) |
|
|
{ |
|
|
return System.Drawing.RectangleF.FromLTRB(rect.Left, rect.Top, rect.Right, rect.Bottom); |
|
|
} |
|
|
|
|
|
public static System.Drawing.Rectangle ToDrawingRect(this SKRectI rect) |
|
|
{ |
|
|
return System.Drawing.Rectangle.FromLTRB(rect.Left, rect.Top, rect.Right, rect.Bottom); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static SKSize ToSKSize(this System.Drawing.SizeF size) |
|
|
{ |
|
|
return new SKSize(size.Width, size.Height); |
|
|
} |
|
|
|
|
|
public static SKSizeI ToSKSize(this System.Drawing.Size size) |
|
|
{ |
|
|
return new SKSizeI(size.Width, size.Height); |
|
|
} |
|
|
|
|
|
public static System.Drawing.SizeF ToDrawingSize(this SKSize size) |
|
|
{ |
|
|
return new System.Drawing.SizeF(size.Width, size.Height); |
|
|
} |
|
|
|
|
|
public static System.Drawing.Size ToDrawingSize(this SKSizeI size) |
|
|
{ |
|
|
return new System.Drawing.Size(size.Width, size.Height); |
|
|
} |
|
|
|
|
|
#if __DESKTOP__ || __WPF__ |
|
|
|
|
|
|
|
|
|
|
|
public static System.Drawing.Bitmap ToBitmap(this SKPicture picture, SKSizeI dimensions) |
|
|
{ |
|
|
using (var image = SKImage.FromPicture(picture, dimensions)) |
|
|
{ |
|
|
return image.ToBitmap(); |
|
|
} |
|
|
} |
|
|
|
|
|
public static System.Drawing.Bitmap ToBitmap(this SKImage skiaImage) |
|
|
{ |
|
|
|
|
|
|
|
|
var bitmap = new System.Drawing.Bitmap(skiaImage.Width, skiaImage.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); |
|
|
var data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, bitmap.PixelFormat); |
|
|
|
|
|
|
|
|
using (var pixmap = new SKPixmap(new SKImageInfo(data.Width, data.Height), data.Scan0, data.Stride)) |
|
|
{ |
|
|
skiaImage.ReadPixels(pixmap, 0, 0); |
|
|
} |
|
|
|
|
|
bitmap.UnlockBits(data); |
|
|
return bitmap; |
|
|
} |
|
|
|
|
|
public static System.Drawing.Bitmap ToBitmap(this SKBitmap skiaBitmap) |
|
|
{ |
|
|
using (var image = SKImage.FromPixels(skiaBitmap.PeekPixels())) |
|
|
{ |
|
|
return image.ToBitmap(); |
|
|
} |
|
|
} |
|
|
|
|
|
public static System.Drawing.Bitmap ToBitmap(this SKPixmap pixmap) |
|
|
{ |
|
|
using (var image = SKImage.FromPixels(pixmap)) |
|
|
{ |
|
|
return image.ToBitmap(); |
|
|
} |
|
|
} |
|
|
|
|
|
public static SKBitmap ToSKBitmap(this System.Drawing.Bitmap bitmap) |
|
|
{ |
|
|
|
|
|
|
|
|
var info = new SKImageInfo(bitmap.Width, bitmap.Height); |
|
|
var skiaBitmap = new SKBitmap(info); |
|
|
using (var pixmap = skiaBitmap.PeekPixels()) |
|
|
{ |
|
|
bitmap.ToSKPixmap(pixmap); |
|
|
} |
|
|
return skiaBitmap; |
|
|
} |
|
|
|
|
|
public static SKImage ToSKImage(this System.Drawing.Bitmap bitmap) |
|
|
{ |
|
|
|
|
|
|
|
|
var info = new SKImageInfo(bitmap.Width, bitmap.Height); |
|
|
var image = SKImage.Create(info); |
|
|
using (var pixmap = image.PeekPixels()) |
|
|
{ |
|
|
bitmap.ToSKPixmap(pixmap); |
|
|
} |
|
|
return image; |
|
|
} |
|
|
|
|
|
public static void ToSKPixmap(this System.Drawing.Bitmap bitmap, SKPixmap pixmap) |
|
|
{ |
|
|
|
|
|
|
|
|
if (pixmap.ColorType == SKImageInfo.PlatformColorType) |
|
|
{ |
|
|
var info = pixmap.Info; |
|
|
using (var tempBitmap = new System.Drawing.Bitmap(info.Width, info.Height, info.RowBytes, System.Drawing.Imaging.PixelFormat.Format32bppPArgb, pixmap.GetPixels())) |
|
|
using (var gr = System.Drawing.Graphics.FromImage(tempBitmap)) |
|
|
{ |
|
|
gr.DrawImageUnscaled(bitmap, 0, 0); |
|
|
} |
|
|
} |
|
|
else |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
using (var tempImage = bitmap.ToSKImage()) |
|
|
{ |
|
|
tempImage.ReadPixels(pixmap, 0, 0); |
|
|
} |
|
|
} |
|
|
} |
|
|
#endif |
|
|
|
|
|
#if __ANDROID__ || __DESKTOP__ || __WPF__ |
|
|
|
|
|
|
|
|
public static SKColor ToSKColor(this System.Drawing.Color color) |
|
|
{ |
|
|
return (SKColor)(uint)color.ToArgb(); |
|
|
} |
|
|
|
|
|
public static System.Drawing.Color ToDrawingColor(this SKColor color) |
|
|
{ |
|
|
return System.Drawing.Color.FromArgb((int)(uint)color); |
|
|
} |
|
|
#endif |
|
|
|
|
|
#endif |
|
|
} |
|
|
} |
|
|
|