protected override void OnPaintSurface(SKPaintGLSurfaceEventArgs args) { base.OnPaintSurface(args); SKSize info = CanvasSize; SKCanvas canvas = args.Surface.Canvas; if (rectInfo.Width != info.Width || rectInfo.Height != info.Height) { rectInfo = new SKRect(0, 0, info.Width, info.Height); SetMainBitmapMatrix(); SetTrashRects(); } var rectImage = SkiaHelper.CalculateRectangle(rectInfo, outImageWidht, outImageHeight).rect; OnPaintSurface(canvas, rectImage, false); canvas.DrawSurrounding(rectInfo, rectImage, SkiaHelper.backgroundColor); DrawTrasRect(canvas); }
protected override void OnPaintSurface(SKPaintSurfaceEventArgs args) { base.OnPaintSurface(args); SKImageInfo info = args.Info; SKSurface surface = args.Surface; SKCanvas canvas = surface.Canvas; canvas.Clear(SkiaHelper.backgroundColor); // 计算显示位图的矩形 var rect = SkiaHelper.CalculateRectangle(new SKRect(0, 0, info.Width, info.Height), bitmap); canvas.DrawBitmap(bitmap, rect.rect); // 计算用于显示裁剪矩形的矩阵变换 SKMatrix bitmapScaleMatrix = SKMatrix.CreateIdentity(); SKMatrix.CreateScaleTranslation(rect.scaleX, rect.scaleX, rect.rect.Left, rect.rect.Top); // 显示矩形 SKRect scaledCropRect = bitmapScaleMatrix.MapRect(croppingRect.Rect); using (SKPaint edgeStroke = new SKPaint()) { edgeStroke.Style = SKPaintStyle.Stroke; edgeStroke.Color = SKColors.White; edgeStroke.StrokeWidth = 3; edgeStroke.IsAntialias = true; canvas.DrawRect(scaledCropRect, edgeStroke); } canvas.DrawSurrounding(rect.rect, scaledCropRect, SKColors.Gray.WithAlpha(190)); // Display heavier corners using (SKPaint cornerStroke = new SKPaint()) using (SKPath path = new SKPath()) { cornerStroke.Style = SKPaintStyle.Stroke; cornerStroke.Color = SKColors.White; cornerStroke.StrokeWidth = 7; path.MoveTo(scaledCropRect.Left, scaledCropRect.Top + corner); path.LineTo(scaledCropRect.Left, scaledCropRect.Top); path.LineTo(scaledCropRect.Left + corner, scaledCropRect.Top); path.MoveTo(scaledCropRect.Right - corner, scaledCropRect.Top); path.LineTo(scaledCropRect.Right, scaledCropRect.Top); path.LineTo(scaledCropRect.Right, scaledCropRect.Top + corner); path.MoveTo(scaledCropRect.Right, scaledCropRect.Bottom - corner); path.LineTo(scaledCropRect.Right, scaledCropRect.Bottom); path.LineTo(scaledCropRect.Right - corner, scaledCropRect.Bottom); path.MoveTo(scaledCropRect.Left + corner, scaledCropRect.Bottom); path.LineTo(scaledCropRect.Left, scaledCropRect.Bottom); path.LineTo(scaledCropRect.Left, scaledCropRect.Bottom - corner); canvas.DrawPath(path, cornerStroke); } // 反转变换以进行触摸跟踪 bitmapScaleMatrix.TryInvert(out inverseBitmapMatrix); }