Image Resize in C# - Algorith to determine resize dimensions (height and width)

后端 未结 5 1149
挽巷
挽巷 2020-12-28 23:32

I need to scale down an image that has a height or width greater than a predefined pixel value.

I wrote some code that takes a look at the original image, checks to

5条回答
  •  执笔经年
    2020-12-29 00:13

    Fitting an image to new size requires two operations:

    1. Resize - resize the source image to fit exactly one dimension (width or height - the one with the smaller ratio)

    2. Crop - crop the result of the previous operation to the target dimensions

    Here is a small sample:

        private static Image Resize(Image img, int width, int height)
        {
            Bitmap b = new Bitmap(width, height);
            using (Graphics g = Graphics.FromImage((Image)b))
            {
                g.DrawImage(img, 0, 0, width, height);
            }
    
            return (Image)b;
        }
    
        public static Image Crop(Image image, int width, int height)
        {
            int cropx = image.Width > width ? image.Width / 2 - width / 2 : 0;
            int cropy = image.Height > height ? image.Height / 2 - height / 2 : 0;
            width = image.Width > width ? width : image.Width;
            height = image.Height > height ? height : image.Height;
    
            Rectangle cropRect = new Rectangle(cropx, cropy, width, height);
    
            var target = new Bitmap(cropRect.Width, cropRect.Height);
    
            using (Graphics g = Graphics.FromImage(target))
            {
                g.DrawImage(image, new Rectangle(0, 0, target.Width, target.Height), cropRect, GraphicsUnit.Pixel);
            }
    
            return target;
        }
    
        public static Image FitToSize(Image image, int width, int height)
        {
            var wratio = 1.0 * image.Width / width;
            var hratio = 1.0 * image.Height / height;
    
            int wresize;
            int hresize;
    
            if (wratio >= hratio && wratio > 1)
            {
                wresize = (int)Math.Round((double)image.Width / hratio);
                hresize = height;
    
                image = Resize(image, wresize, hresize);
                image = Crop(image, width, height);  
            }
            else if (hratio >= wratio && hratio > 1)
            {
                hresize = (int)Math.Round((double)image.Height / wratio);
                wresize = width;
    
                image = Resize(image, wresize, hresize);
                image = Crop(image, width, height);
            }
            return image;
    
        }
    

提交回复
热议问题