public static void DrawBitmap(this SKCanvas canvas, SKBitmap bitmap, SKRect source, SKRect dest, BitmapStretch stretch, BitmapAlignment horizontal = BitmapAlignment.Center, BitmapAlignment vertical = BitmapAlignment.Center, SKPaint paint = null) { if (stretch == BitmapStretch.Fill) { canvas.DrawBitmap(bitmap, source, dest, paint); } else { float scale = 1; switch (stretch) { case BitmapStretch.None: break; case BitmapStretch.Uniform: scale = Math.Min(dest.Width / source.Width, dest.Height / source.Height); break; case BitmapStretch.UniformToFill: scale = Math.Max(dest.Width / source.Width, dest.Height / source.Height); break; } SKRect display = CalculateDisplayRect(dest, scale * source.Width, scale * source.Height, horizontal, vertical); canvas.DrawBitmap(bitmap, source, display, paint); } }
void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args) { SKImageInfo info = args.Info; SKSurface surface = args.Surface; SKCanvas canvas = surface.Canvas; canvas.Clear(); SKRect dest = new SKRect(0, 0, info.Width, info.Height); BitmapStretch stretch = (BitmapStretch)stretchPicker.SelectedItem; BitmapAlignment horizontal = (BitmapAlignment)horizontalPicker.SelectedItem; BitmapAlignment vertical = (BitmapAlignment)verticalPicker.SelectedItem; canvas.DrawBitmap(bitmap, SOURCE, dest, stretch, horizontal, vertical); }
static SKRect CalculateDisplayRect(SKRect dest, float bmpWidth, float bmpHeight, BitmapAlignment horizontal, BitmapAlignment vertical) { float x = 0; float y = 0; switch (horizontal) { case BitmapAlignment.Center: x = (dest.Width - bmpWidth) / 2; break; case BitmapAlignment.Start: break; case BitmapAlignment.End: x = dest.Width - bmpWidth; break; } switch (vertical) { case BitmapAlignment.Center: y = (dest.Height - bmpHeight) / 2; break; case BitmapAlignment.Start: break; case BitmapAlignment.End: y = dest.Height - bmpHeight; break; } x += dest.Left; y += dest.Top; return(new SKRect(x, y, x + bmpWidth, y + bmpHeight)); }