public static Bitmap ScaleImage(NRandom rsz_rng, Image image, int width, int height)
        {
            float aspectRatio = (float)image.Width / image.Height;

            float height_var = (height - (int)(height * aspectRatio)) * 0.8f;
            float width_var  = (width - (int)(width / aspectRatio)) * 0.8f;

            var destRect = new Rectangle((int)(height_var * rsz_rng.NextDouble()), 0, (int)(height * aspectRatio), width);

            if (aspectRatio > 1.0f)
            {
                destRect = new Rectangle(0, (int)(width_var * rsz_rng.NextDouble()), height, (int)(width / aspectRatio));
            }
            var destImage = new Bitmap(width, height);

            destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);

            using (var graphics = Graphics.FromImage(destImage))
            {
                graphics.CompositingMode    = CompositingMode.SourceCopy;
                graphics.CompositingQuality = CompositingQuality.HighQuality;
                graphics.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                graphics.SmoothingMode      = SmoothingMode.HighQuality;
                graphics.PixelOffsetMode    = PixelOffsetMode.HighQuality;

                using (var wrapMode = new ImageAttributes())
                {
                    wrapMode.SetWrapMode(WrapMode.TileFlipXY);
                    graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
                }
            }

            return(destImage);
        }
Example #2
0
 public NRandom(NRandom r)
 {
     this.seed = r.seed;
     lock_obj  = new object();
 }