/// <summary> /// Creates a scaled version of a <see cref="System.Drawing.Image"/>. /// </summary> /// <param name="original">The original <see cref="System.Drawing.Image"/>.</param> /// <param name="destWidth">The target width.</param> /// <param name="destHeight">The target height.</param> /// <param name="keepAspectRatio">If true, the image will maintain the aspect ratio.</param> /// <param name="bgColor">The background color of the new image.</param> /// <param name="transparentColor">The color to make transparent in the new image.</param> /// <returns> /// A <see cref="Bitmap"/> resized to the given values. /// </returns> public static Bitmap CreateScaled(this System.Drawing.Image original, int destWidth, int destHeight, bool keepAspectRatio, Color?bgColor, Color?transparentColor) { var srcRect = new Rectangle(0, 0, original.Width, original.Height); return(CreateScaled(original, srcRect, destWidth, destHeight, keepAspectRatio, bgColor, transparentColor)); }
/// <summary> /// Creates a <see cref="SFML.Graphics.Image"/> from a <see cref="System.Drawing.Image"/>. /// </summary> /// <param name="img">The <see cref="System.Drawing.Image"/>.</param> /// <returns>The <see cref="SFML.Graphics.Image"/>.</returns> public static Image ToSFMLImage(this System.Drawing.Image img) { using (var ms = new MemoryStream((img.Width * img.Height * 4) + 64)) { img.Save(ms, ImageFormat.Bmp); return(new Image(ms)); } }
/// <summary> /// Creates a scaled version of a <see cref="System.Drawing.Image"/>. /// </summary> /// <param name="original">The original <see cref="System.Drawing.Image"/>.</param> /// <param name="srcRect">The source image rectangle.</param> /// <param name="destWidth">The target width.</param> /// <param name="destHeight">The target height.</param> /// <param name="keepAspectRatio">If true, the image will maintain the aspect ratio.</param> /// <param name="bgColor">The background color of the new image.</param> /// <param name="transparentColor">The color to make transparent in the new image.</param> /// <returns> /// A <see cref="Bitmap"/> resized to the given values. /// </returns> public static Bitmap CreateScaled(this System.Drawing.Image original, Rectangle srcRect, int destWidth, int destHeight, bool keepAspectRatio, Color?bgColor, Color?transparentColor) { int w; int h; if (keepAspectRatio) { // Get the destination size, maintaining aspect ratio var aspectRatio = (float)original.Width / original.Height; if (aspectRatio > 1) { w = destWidth; h = (int)(destHeight * (1f / aspectRatio)); if (h <= 4 && destHeight >= 4) { h = 4; } } else { w = (int)(destWidth * aspectRatio); h = destHeight; if (w <= 4 && destWidth >= 4) { w = 4; } } } else { // Don't maintain aspect ratio w = destWidth; h = destHeight; } Debug.Assert(w <= destWidth); Debug.Assert(h <= destHeight); // Center var x = (int)((destWidth - w) / 2f); var y = (int)((destHeight - h) / 2f); // Create the new bitmap to return var ret = new Bitmap(destWidth, destHeight); using (var g = System.Drawing.Graphics.FromImage(ret)) { g.PixelOffsetMode = _defaultPixelOffsetMode; g.CompositingQuality = _defaultCompositingQuality; g.InterpolationMode = _defaultInterpolationMode; g.SmoothingMode = _defaultSmoothingMode; if (bgColor.HasValue) { g.Clear(bgColor.Value); } var destRect = new Rectangle(x, y, w, h); g.DrawImage(original, destRect, srcRect, GraphicsUnit.Pixel); } if (transparentColor.HasValue) { ret.MakeTransparent(transparentColor.Value); } Debug.Assert(ret.Width == destWidth); Debug.Assert(ret.Height == destHeight); return(ret); }