/// <summary> /// 创建缩略图,未格式化 /// </summary> /// <param name="abPath"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="mode"></param> /// <returns></returns> public static SKBitmap MakeThumb(string abPath, int width, int height, CroppingMode mode) { using (var input = System.IO.File.OpenRead(abPath)) { if (mode == CroppingMode.NONE) { return(SKBitmap.Decode(input)); } using (var inputStream = new SKManagedStream(input)) { using (var bitmap = SKBitmap.Decode(inputStream)) { var cropRect = new CroppingRectangle(bitmap, width, height, mode); SKBitmap croppedBitmap = new SKBitmap((int)cropRect.Dest.Width, (int)cropRect.Dest.Height); using (SKCanvas canvas = new SKCanvas(croppedBitmap)) { canvas.DrawBitmap(bitmap, cropRect.Rect, cropRect.Dest); } return(croppedBitmap); } } } }
/// <summary> /// 创建缩略图,并且格式化 /// </summary> /// <param name="abPath"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="mode"></param> /// <param name="format"></param> /// <param name="stream">水印流</param> /// <returns></returns> public static SKData MakeThumb(string abPath, int width, int height, CroppingMode mode, SKEncodedImageFormat format, Stream stream) { using (var input = File.OpenRead(abPath)) { SKBitmap bitmap = null; SKCanvas canvas = null; if (mode == CroppingMode.NONE) { bitmap = SKBitmap.Decode(input); canvas = new SKCanvas(bitmap); } else { var source = SKBitmap.Decode(input); var cropRect = new CroppingRectangle(source, width, height, mode); bitmap = new SKBitmap((int)cropRect.Dest.Width, (int)cropRect.Dest.Height); canvas = new SKCanvas(bitmap); canvas.DrawBitmap(source, cropRect.Rect, cropRect.Dest); } if (stream != null) { DrawMark(canvas, bitmap.Width, bitmap.Height, stream); } SKData data = SKImage.FromBitmap(bitmap).Encode(format, QUALITY); canvas.Dispose(); bitmap.Dispose(); return(data); } }
public CroppingRectangle(SKBitmap shourceBitmap, int width, int height, CroppingMode croppingMode) { SKRect dest = new SKRect(0, 0, width, height); SKRect rect = new SKRect(0, 0, shourceBitmap.Width, shourceBitmap.Height); switch (croppingMode) { case CroppingMode.RATIO_WIDTH: dest.Bottom = rect.Bottom * width / rect.Right; break; case CroppingMode.RATIO_HEIGHT: dest.Right = rect.Right * height / rect.Bottom; break; case CroppingMode.CUT: SKRect maxRect = new SKRect(0, 0, shourceBitmap.Width, shourceBitmap.Height); float oRight = maxRect.Right, oBottom = maxRect.Bottom; //纵向比计算 if (oRight / oBottom > (float)width / height) { rect.Top = 0; float w = rect.Bottom * width / height; rect.Left = (rect.Width - w) / 2; rect.Right = rect.Left + w; } else { rect.Left = 0; float h = rect.Right * height / width; rect.Top = (rect.Bottom - h) / 2; rect.Bottom = rect.Top + h; } break; } Rect = rect; Dest = dest; }
/// <summary> /// /// </summary> /// <param name="cut"></param> /// <param name="resize"></param> public ResolveArg(string cut, string resize) { CroppingMode mode = CroppingMode.NONE; int width = 0, height = 0; if (!String.IsNullOrEmpty(resize)) { string w, h; if (resize.IndexOf('x') > -1) { var arr = resize.Split("x"); for (int i = 0; i < arr.Length; i++) { if (arr[i].StartsWith("w_")) { w = arr[i].Replace("w_", ""); int.TryParse(w, out width); } else { h = arr[i].Replace("h_", ""); int.TryParse(h, out height); } } } else if (resize.StartsWith("h_")) { h = resize.Replace("h_", ""); int.TryParse(h, out height); } else if (resize.StartsWith("w_")) { w = resize.Replace("w_", ""); int.TryParse(w, out width); } switch (cut) { case "m_fill": if (width > 0 && height > 0) { mode = CroppingMode.CUT; } break; case "m_h": if (height > 0) { mode = CroppingMode.RATIO_HEIGHT; } break; case "m_w": if (width > 0) { mode = CroppingMode.RATIO_WIDTH; } break; } } this.Width = width; this.Height = height; this.Mode = mode; }