An advanced sprite packing tool. Currently a work in progress.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.1KB

  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. namespace SpritePacker.SBRLUtilities
  5. {
  6. public static partial class ImageTools
  7. {
  8. /// <summary>
  9. /// Resizes a <c>Bitmap</c> such that it fits within the target dimensions.
  10. /// </summary>
  11. /// <param name="sourceImage">The image to resize.</param>
  12. /// <param name="targetBox">The target dimensions.</param>
  13. /// <returns>The resized image.</returns>
  14. public static Bitmap ResizeImage(Bitmap sourceImage, Size targetBox)
  15. {
  16. float scaleFactor = Math.Min(
  17. targetBox.Width / (float)sourceImage.Width,
  18. targetBox.Height / (float)sourceImage.Height
  19. );
  20. Size thumbnailSize = new Size(
  21. (int)(sourceImage.Width * scaleFactor),
  22. (int)(sourceImage.Height * scaleFactor)
  23. );
  24. Bitmap resultImage = new Bitmap(thumbnailSize.Width, thumbnailSize.Height);
  25. using (Graphics context = Graphics.FromImage(resultImage)) {
  26. context.CompositingMode = CompositingMode.SourceCopy;
  27. context.InterpolationMode = InterpolationMode.HighQualityBicubic;
  28. context.DrawImage(sourceImage, new Rectangle(
  29. Point.Empty,
  30. thumbnailSize
  31. ));
  32. }
  33. return resultImage;
  34. }
  35. }
  36. }