图像缩放不起作用原始图像的高度和放大器时;宽度较小的缩放高度&放大器;宽度 [英] Image scaling does not work when original image height & width is smaller the scaling height & width

查看:112
本文介绍了图像缩放不起作用原始图像的高度和放大器时;宽度较小的缩放高度&放大器;宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试缩放图像时,我有一个问题。问题presents本身,当我试图扩展(原创)图像比大小我试图扩展到更小的。

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.

例如,具有850像素的宽度和700像素高度的图像试图扩大规模至950像素的宽度和高度。图像似乎是适当的比例,但beeing画错了我的位​​图。在code缩放图像将随之而来。宽度beeing送入ScaleToFitInside是宽度和高度试图扩展到,950像素都在我的示例

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;
        }

所以画面被缩放,但是beeing画错了我的位​​图从而导致影像落我在位图外,而不是presenting其整个的自我。

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.

例如:当值2000×2000发送(试图高档)。发生以下情况。

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

由于原始图象比升迁值我不想sacle起来,而要保持相同大小。我想要的效果是做绘制的2000×2000大小的矩形,并在它的中心绘制图像。

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:

宽度= 2000;高度= 2000;
resized.Width将是1000和resized.Height将是737(原始图像大小)。

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

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.

先谢谢了。

推荐答案

您要明白以下2个变量将是负面的,当你升迁?

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);

这篇关于图像缩放不起作用原始图像的高度和放大器时;宽度较小的缩放高度&放大器;宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆