Image scaling does not work when original image height & width is smaller the scaling height & width

落花浮王杯 提交于 2020-01-03 03:42:24

问题


I have got a problem when trying to scale an image. The problem presents itself when the image i am trying to scale (original) is smaller than the size i am trying to scale up to.

For example, an image with the width of 850px and the height of 700px trying to be scaled up to 950px width and height. The image seems to be scaled properly but is beeing drawn wrong on my bitmap. The code to scale the image will follow. The width beeing sent into ScaleToFitInside is the width and height trying to scale to, 950px both in my example.

 public static Image ScaleToFitInside(Image image, int width, int height) {
                Image reszied = ScaleToFit(image, width, height);

            Bitmap bitmap = new Bitmap(width, height);
            using (Graphics g = Graphics.FromImage(bitmap)) {
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.PixelOffsetMode = PixelOffsetMode.HighQuality;

                Rectangle rect = new Rectangle(0, 0, width, height);
                g.FillRectangle(Brushes.White, rect);

                int x = (int)(((float)width - (float)reszied.Width) / 2);
                int y = (int)(((float)height - (float)reszied.Height) / 2);
                Point p = new Point(x, y);
                g.DrawImageUnscaled(reszied, p);

                foreach (PropertyItem item in image.PropertyItems) {
                    bitmap.SetPropertyItem(item);
                }
            }

            return bitmap;
        }

            public static Image ScaleToFit(Image image, int maxWidth, int maxHeight) {
            int width = image.Width;
            int height = image.Height;
            float scale = Math.Min(
                ((float)maxWidth / (float)width),
                ((float)maxHeight / (float)height));

            return (scale < 1) ? Resize(image, (int)(width * scale), (int)(height * scale)) : image;
        }

public static Image Resize(Image image, int width, int height) {
            Bitmap bitmap = new Bitmap(width, height);
            using (Graphics g = Graphics.FromImage(bitmap)) {
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.PixelOffsetMode = PixelOffsetMode.HighQuality;

                Rectangle rect = new Rectangle(0, 0, width, height);
                g.DrawImage(image, rect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);

                foreach (PropertyItem item in image.PropertyItems) {
                    bitmap.SetPropertyItem(item);
                }
            }
            return bitmap;
        }

So the picture gets scaled, but beeing drawn wrong on my bitmap causing the image to "fall" outside of my bitmap and not presenting its whole self.

Example: When sending in the values 2000x2000 (trying to upscale). The following happens.

Since the original picture is smaller than the upsizing values i do not want to "sacle it up", but to remain the same size. The effect i want is do draw a rectangle with the size of 2000x2000 and draw the image in the center of it.

My example picture produces these values:

width = 2000; height = 2000; resized.Width will be 1000 and resized.Height will be 737 (original pictures size).

x = (2000 - 1000) / 2 = 500; y = (2000 - 737) / 2 = 631.

When drawing these values onto a paper and matching it to the rectangle it seems to be the right values, but the image is still drawn on th wrong spot, not in the middle at all.

Thanks in advance.


回答1:


You do realize the following 2 variables will be negative when you 'upsize'?

int x = (int)(((float)width - (float)reszied.Width) / 2);
int y = (int)(((float)height - (float)reszied.Height) / 2);


来源:https://stackoverflow.com/questions/3659347/image-scaling-does-not-work-when-original-image-height-width-is-smaller-the-sc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!