File size: 5,742 Bytes
b1b3bae | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | using System;
namespace SkiaSharp.Views.Desktop
{
public static class Extensions
{
private static readonly Lazy<bool> isValidEnvironment = new Lazy<bool>(() =>
{
try
{
// test an operation that requires the native library
SKPMColor.PreMultiply(SKColors.Black);
return true;
}
catch (DllNotFoundException)
{
// If we can't load the native library,
// we may be in some designer.
// We can make this assumption since any other member will fail
// at some point in the draw operation.
return false;
}
});
internal static bool IsValidEnvironment => isValidEnvironment.Value;
#if !WINDOWS_UWP
// System.Drawing.Point*
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);
}
// System.Drawing.Rectangle*
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);
}
// System.Drawing.Size*
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__
// System.Drawing.Bitmap
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)
{
// TODO: maybe keep the same color types where we can, instead of just going to the platform default
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);
// copy
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)
{
// TODO: maybe keep the same color types where we can, instead of just going to the platform default
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)
{
// TODO: maybe keep the same color types where we can, instead of just going to the platform default
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)
{
// TODO: maybe keep the same color types where we can, instead of just going to the platform default
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
{
// we have to copy the pixels into a format that we understand
// and then into a desired format
// TODO: we can still do a bit more for other cases where the color types are the same
using (var tempImage = bitmap.ToSKImage())
{
tempImage.ReadPixels(pixmap, 0, 0);
}
}
}
#endif
#if __ANDROID__ || __DESKTOP__ || __WPF__
// System.Drawing.Color
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
}
}
|