Example #1
0
        /// <summary>
        /// See base docs.
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="mode"></param>
        /// <param name="zoomBackground"></param>
        /// <param name="preferSpeedOverQuality"></param>
        /// <returns></returns>
        public override VrsDrawing.IImage Resize(int width, int height, VrsDrawing.ResizeMode mode = VrsDrawing.ResizeMode.Normal, VrsDrawing.IBrush zoomBackground = null, bool preferSpeedOverQuality = true)
        {
            var result = new Bitmap(width, height, PixelFormat.Format32bppArgb);
            var zoomBackgroundBrushWrapper = zoomBackground as BrushWrapper;

            GdiPlusLock.EnforceSingleThread(() => {
                using (var graphics = Graphics.FromImage(result)) {
                    graphics.SmoothingMode     = SmoothingMode.AntiAlias;
                    graphics.InterpolationMode = preferSpeedOverQuality ? InterpolationMode.Bicubic : InterpolationMode.HighQualityBicubic;
                    graphics.PixelOffsetMode   = PixelOffsetMode.HighQuality;

                    var newWidth  = width;
                    var newHeight = height;
                    var left      = 0;
                    var top       = 0;

                    if (mode != VrsDrawing.ResizeMode.Stretch)
                    {
                        var widthPercent  = (double)newWidth / (double)NativeImage.Width;
                        var heightPercent = (double)newHeight / (double)NativeImage.Height;

                        switch (mode)
                        {
                        case VrsDrawing.ResizeMode.Zoom:
                            // Resize the longest side by the smallest percentage
                            graphics.FillRectangle(zoomBackgroundBrushWrapper.NativeBrush, 0, 0, result.Width, result.Height);
                            if (widthPercent > heightPercent)
                            {
                                newWidth = Math.Min(newWidth, (int)(((double)NativeImage.Width * heightPercent) + 0.5));
                            }
                            else if (heightPercent > widthPercent)
                            {
                                newHeight = Math.Min(newHeight, (int)(((double)NativeImage.Height * widthPercent) + 0.5));
                            }
                            break;

                        case VrsDrawing.ResizeMode.Normal:
                        case VrsDrawing.ResizeMode.Centre:
                            // Resize the smallest side by the largest percentage
                            if (widthPercent > heightPercent)
                            {
                                newHeight = Math.Max(newHeight, (int)(((double)NativeImage.Height * widthPercent) + 0.5));
                            }
                            else if (heightPercent > widthPercent)
                            {
                                newWidth = Math.Max(newWidth, (int)(((double)NativeImage.Width * heightPercent) + 0.5));
                            }
                            break;
                        }

                        if (mode != VrsDrawing.ResizeMode.Normal)
                        {
                            left = (width - newWidth) / 2;
                            top  = (height - newHeight) / 2;
                        }
                    }

                    graphics.DrawImage(NativeImage, left, top, newWidth, newHeight);
                }
            });

            return(new ImageWrapper(result));
        }
Example #2
0
 /// <summary>
 /// See interface docs.
 /// </summary>
 /// <param name="width"></param>
 /// <param name="height"></param>
 /// <param name="mode"></param>
 /// <param name="zoomBackground"></param>
 /// <param name="preferSpeedOverQuality"></param>
 /// <returns></returns>
 public abstract VrsDrawing.IImage Resize(int width, int height, VrsDrawing.ResizeMode mode = VrsDrawing.ResizeMode.Normal, VrsDrawing.IBrush zoomBackground = null, bool preferSpeedOverQuality = true);