示例#1
0
        /// <summary>
        /// http://www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-saving-cropping-and-resizing
        /// </summary>
        /// <param name="image"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public static Image ImageResize(Image image, Size size, DominantMeasure dominantMeasure = DominantMeasure.Auto)
        {
            int sourceWidth  = image.Width;
            int sourceHeight = image.Height;

            float nPercent  = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)size.Width / (float)sourceWidth);
            nPercentH = ((float)size.Height / (float)sourceHeight);

            #region calcolo misura dominante (larghezza o altezza)
            if (dominantMeasure == DominantMeasure.Auto)
            {
                if (nPercentH < nPercentW)
                {
                    nPercent = nPercentH;
                }
                else
                {
                    nPercent = nPercentW;
                }
            }
            else if (dominantMeasure == DominantMeasure.Width)
            {
                nPercent = nPercentW;
            }
            else if (dominantMeasure == DominantMeasure.Height)
            {
                nPercent = nPercentH;
            }
            #endregion

            int destWidth  = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap   b = new Bitmap(destWidth, destHeight);
            Graphics g = Graphics.FromImage((Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.DrawImage(image, 0, 0, destWidth, destHeight);
            g.Dispose();

            return((Image)b);
        }
示例#2
0
        /// <summary>
        /// http://www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-saving-cropping-and-resizing
        /// </summary>
        /// <param name="image"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public static Image ImageResize(Image image, Size size, DominantMeasure dominantMeasure = DominantMeasure.Auto)
        {
            int sourceWidth = image.Width;
            int sourceHeight = image.Height;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)size.Width / (float)sourceWidth);
            nPercentH = ((float)size.Height / (float)sourceHeight);

            #region calcolo misura dominante (larghezza o altezza)
            if (dominantMeasure == DominantMeasure.Auto)
            {
                if (nPercentH < nPercentW)
                    nPercent = nPercentH;
                else
                    nPercent = nPercentW;
            }
            else if (dominantMeasure == DominantMeasure.Width)
                nPercent = nPercentW;
            else if (dominantMeasure == DominantMeasure.Height)
                nPercent = nPercentH;
            #endregion

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap b = new Bitmap(destWidth, destHeight);
            Graphics g = Graphics.FromImage((Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.DrawImage(image, 0, 0, destWidth, destHeight);
            g.Dispose();

            return (Image)b;
        }