Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.27 KB | None | 0 0
  1.  private static Stream ResizeImage(Stream fileimage, int maxWidth, int maxHeight)
  2.         {
  3.             fileimage.Position = 0;
  4.             using (var bitmap = SKBitmap.Decode(fileimage))
  5.             {
  6.                 var ratioX = (float) maxWidth / bitmap.Width;
  7.                 var ratioY = (float) maxHeight / bitmap.Height;
  8.                 var ratio = Math.Min(ratioX, ratioY);
  9.  
  10.                 using (var toBitmap = new SKBitmap((int) Math.Round(bitmap.Width * ratio),
  11.                     (int) Math.Round(bitmap.Height * ratio), bitmap.ColorType, bitmap.AlphaType))
  12.                 using (var canvas = new SKCanvas(toBitmap))
  13.                 {
  14.                     canvas.SetMatrix(SKMatrix.MakeScale(ratio, ratio));
  15.                     canvas.DrawBitmap(bitmap, 0, 0);
  16.                     canvas.ResetMatrix();
  17.                     canvas.Flush();
  18.                     canvas.Dispose();
  19.  
  20.                     using (var image = SKImage.FromBitmap(toBitmap))
  21.                     using (var data = image.Encode(SKEncodedImageFormat.Png, 100))
  22.                     {
  23.                         var stream = new MemoryStream();
  24.                         data.SaveTo(stream);
  25.                         return stream;
  26.                     }
  27.                 }
  28.             }
  29.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement