/// <summary> /// 返回实际占用的Rect 和 scale /// </summary> public static DrawBitmapResult DrawBitmap(this SKCanvas canvas, SKBitmap bitmap, SKRect dest, SKStretchMode stretch, SKAlignment horizontal = SKAlignment.Center, SKAlignment vertical = SKAlignment.Center, SKPaint?paint = null) { if (stretch == SKStretchMode.Fill) { canvas.DrawBitmap(bitmap, dest, paint); return(new DrawBitmapResult(dest, dest.Width / bitmap.Width, dest.Height / bitmap.Height)); } float scale = 1; switch (stretch) { case SKStretchMode.None: break; case SKStretchMode.AspectFit: scale = Math.Min(dest.Width / bitmap.Width, dest.Height / bitmap.Height); break; case SKStretchMode.AspectFill: scale = Math.Max(dest.Width / bitmap.Width, dest.Height / bitmap.Height); break; } SKRect display = CalculateDisplayRect(dest, scale * bitmap.Width, scale * bitmap.Height, horizontal, vertical); canvas.DrawBitmap(bitmap, display, paint); return(new DrawBitmapResult(display, scale, scale)); }
public static void DrawBitmap(this SKCanvas canvas, SKBitmap bitmap, SKRect dest, SKStretch stretch, SKAlignment horizontal = SKAlignment.Center, SKAlignment vertical = SKAlignment.Center, SKPaint paint = null) { if (stretch == SKStretch.Fill) { canvas.DrawBitmap(bitmap, dest, paint); } else { float scale = 1; switch (stretch) { case SKStretch.None: break; case SKStretch.Uniform: scale = Math.Min(dest.Width / bitmap.Width, dest.Height / bitmap.Height); break; case SKStretch.UniformToFill: scale = Math.Max(dest.Width / bitmap.Width, dest.Height / bitmap.Height); break; } SKRect display = CalculateDisplayRect(dest, scale * bitmap.Width, scale * bitmap.Height, horizontal, vertical); canvas.DrawBitmap(bitmap, display, paint); } }
static SKRect CalculateDisplayRect(SKRect dest, float bmpWidth, float bmpHeight, SKAlignment horizontal, SKAlignment vertical) { float x = 0; float y = 0; switch (horizontal) { case SKAlignment.Center: x = (dest.Width - bmpWidth) / 2; break; case SKAlignment.Start: break; case SKAlignment.End: x = dest.Width - bmpWidth; break; } switch (vertical) { case SKAlignment.Center: y = (dest.Height - bmpHeight) / 2; break; case SKAlignment.Start: break; case SKAlignment.End: y = dest.Height - bmpHeight; break; } x += dest.Left; y += dest.Top; return(new SKRect(x, y, x + bmpWidth, y + bmpHeight)); }