Beispiel #1
0
 /// <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);
     }
 }
Beispiel #2
0
        /// <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);
                    }
                }
            }
        }