示例#1
0
        public static Bitmap Cut(this Image source, Rectangle destrect, ImageQuality quality)
        {
            Bitmap target = null;

            try
            {
                target = new Bitmap(destrect.Width, destrect.Height);

                using (var gh = Graphics.FromImage(target))
                {
                    gh.CompositingQuality = quality.Compositing;
                    gh.SmoothingMode      = quality.Smoothing;
                    gh.InterpolationMode  = quality.Interpolation;
                    gh.PixelOffsetMode    = quality.PixelOffset;

                    gh.DrawImage(source, destrect, 0, 0, destrect.Width,
                                 destrect.Height, GraphicsUnit.Pixel);

                    gh.DrawImage(source, new Rectangle(0, 0, destrect.Width, destrect.Height), destrect, GraphicsUnit.Pixel);
                }
            }
            catch (Exception ex)
            {
                if (target != null)
                {
                    target.Dispose();
                }
                throw ex;
            }
            return(target);
        }
示例#2
0
        /// <summary>
        /// 根据指定的宽高,等比缩放图片,多余的部分会以白边补全
        /// </summary>
        /// <param name="source"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="quality">质量,这个参数并不太会影响图像的呈现质量,主要是可以提高速度,质量越低速度越快</param>
        /// <param name="background"></param>
        /// <returns></returns>
        public static Bitmap Scale(this Image source, int width, int height, ImageQuality quality, Color background)
        {
            var rawRect = new DynamicRectangle()
            {
                X      = 0,
                Y      = 0,
                Width  = source.Width,
                Height = source.Height
            };

            var targetRect = rawRect.Scale(width, height, true);

            Bitmap target = null;

            try
            {
                target = new Bitmap(width, height);

                using (var gh = Graphics.FromImage(target))
                {
                    gh.CompositingQuality = quality.Compositing;
                    gh.SmoothingMode      = quality.Smoothing;
                    gh.InterpolationMode  = quality.Interpolation;
                    gh.PixelOffsetMode    = quality.PixelOffset;
                    //gh.FillRectangle(new SolidBrush(background), 0, 0, width, height);

                    //清空画布并以指定的背景色填充
                    gh.Clear(background);

                    Rectangle destrect = targetRect.ToRectangle();
                    gh.DrawImage(source, destrect, 0, 0, source.Width,
                                 source.Height, GraphicsUnit.Pixel);
                }
            }
            catch (Exception ex)
            {
                if (target != null)
                {
                    target.Dispose();
                }
                throw ex;
            }
            return(target);
        }