9public static class SkiaExtensions
16 public static Rectangle ToRectangle(
this SKRect rect)
28 public static SKColor ToSKColor(
this Color color)
40 public static Color ToColor(
this SKColor skcolor)
52 public static SKRect ToSKRect(
this Rectangle rect)
57 rect.Y + rect.Height);
64 public static SKRect ToSKRect(
this RectangleF rect)
69 rect.Y + rect.Height);
76 public static SKRectI ToSKRectI(
this Rectangle rect)
89 public static SKPoint[] ToSKPoints(
this Point[] points,
bool enclose =
false)
92 return points.Select(p =>
new SKPoint(p.X, p.Y)).ToArray();
96 var len = points.Length + 1;
97 var result =
new SKPoint[len];
99 for (
int i = 0; i < points.Length; i++)
100 result[i] =
new SKPoint(points[i].X, points[i].Y);
103 result[^1] = result[0];
114 public static SKBitmap CopyRegion(
this SKBitmap source, Rectangle sourceRect)
116 if (source ==
null || source.IsEmpty)
117 throw new ArgumentException(
"Invalid source bitmap.", nameof(source));
119 if (sourceRect.Width <= 0 || sourceRect.Height <= 0)
120 throw new ArgumentException(
"Source rectangle must have positive size.", nameof(sourceRect));
122 var skSourceRect = sourceRect.ToSKRectI();
124 if (!source.Info.Rect.Contains(skSourceRect))
125 throw new ArgumentOutOfRangeException(nameof(sourceRect),
"Source rectangle is outside the bitmap bounds.");
127 var copy =
new SKBitmap(
131 source.Info.ColorType,
132 source.Info.AlphaType));
134 using var canvas =
new SKCanvas(copy);
136 canvas.Clear(SKColors.Transparent);
156 public static SKImage CopyRegion(
this SKImage source, Rectangle sourceRect)
159 throw new ArgumentNullException(nameof(source));
161 if (sourceRect.Width <= 0 || sourceRect.Height <= 0)
162 throw new ArgumentException(
"Source rectangle must have positive size.", nameof(sourceRect));
164 var skSourceRect =
new SKRect(
170 var skDestRect =
new SKRect(
176 var imageInfo =
new SKImageInfo(
183 using var surface = SKSurface.Create(imageInfo)
184 ??
throw new InvalidOperationException(
"Could not create SKSurface.");
186 surface.Canvas.Clear(SKColors.Transparent);
188 surface.Canvas.DrawImage(
193 return surface.Snapshot();
203 public static void SaveBitmapToFile(
204 this SKBitmap bitmap,
206 SKEncodedImageFormat format = SKEncodedImageFormat.Png,
209 if (bitmap ==
null || bitmap.IsEmpty)
210 throw new ArgumentException(
"Invalid bitmap.");
212 using var image = SKImage.FromBitmap(bitmap);
213 using var data = image.Encode(format, quality);
214 using var stream = File.OpenWrite(filePath);
225 public static byte[] EncodeBitmapToBytes(
226 this SKBitmap bitmap,
227 SKEncodedImageFormat format = SKEncodedImageFormat.Png,
230 if (bitmap ==
null || bitmap.IsEmpty)
231 throw new ArgumentException(
"Invalid bitmap.");
233 using var image = SKImage.FromBitmap(bitmap);
234 using var data = image.Encode(format, quality);
235 return data.ToArray();
245 public static void ApplyAlphaMask(
this SKBitmap bitmap, SKColor targetColor,
byte tolerance = 5)
247 if (bitmap ==
null || bitmap.IsEmpty)
248 throw new ArgumentException(
"Invalid bitmap.");
250 int width = bitmap.Width;
251 int height = bitmap.Height;
253 for (
int y = 0; y < height; y++)
255 for (
int x = 0; x < width; x++)
257 var color = bitmap.GetPixel(x, y);
259 if (IsColorClose(color, targetColor, tolerance))
274 bitmap.NotifyPixelsChanged();
284 public static SKBitmap PremultiplyAlpha(
this SKBitmap bitmap)
286 if (bitmap ==
null || bitmap.IsEmpty)
287 throw new ArgumentException(
"Invalid bitmap.");
289 if (bitmap.AlphaType == SKAlphaType.Premul)
292 var info =
new SKImageInfo(
298 var premulBitmap =
new SKBitmap(info);
300 int width = bitmap.Width;
301 int height = bitmap.Height;
303 for (
int y = 0; y < height; y++)
305 for (
int x = 0; x < width; x++)
307 var c = bitmap.GetPixel(x, y);
310 byte r = (byte)((c.Red * a + 127) / 255);
311 byte g = (byte)((c.Green * a + 127) / 255);
312 byte b = (byte)((c.Blue * a + 127) / 255);
314 premulBitmap.SetPixel(
325 premulBitmap.NotifyPixelsChanged();
329 private static bool IsColorClose(SKColor a, SKColor b,
byte tolerance)
332 Math.Abs(a.Red - b.Red) <= tolerance &&
333 Math.Abs(a.Green - b.Green) <= tolerance &&
334 Math.Abs(a.Blue - b.Blue) <= tolerance;
@ Rectangle
Particles are spawned uniformly within a rectangular area. This is the default distribution.