二进制图像大小调整出错

本文关键字:调整 出错 图像 二进制 | 更新日期: 2023-09-27 18:00:51

如果需要的话,我会尝试将二进制图像调整为较小的文件,但所有图像在调整大小后都会增加字节,而大小却越来越小,所以结果是非常难看的图像。。。不知道为什么它会变大。

这是我使用的代码,如有任何帮助,将不胜感激。

using (var srcImage = System.Drawing.Image.FromStream(myMemStream))
        {
            double height = srcImage.Height;
            double width = srcImage.Width;
            newWidth = (int)(srcImage.Width);
            double aspect = scale / width;
            newHeight = Convert.ToInt32(aspect * height);
            newWidth = Convert.ToInt32(aspect * width);
            using (var newImage = new Bitmap(newWidth, newHeight))
            using (var graphics = Graphics.FromImage(newImage))
            {
                graphics.SmoothingMode = SmoothingMode.AntiAlias;
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
                graphics.DrawImage(srcImage, new Rectangle(0, 0, newWidth, newHeight));
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                newImage.Save(ms, format);
                ms.Position = 0;
                _bytes = ms.ToArray();  //Returns a new byte array.
                newImage.Dispose();
            }
        }

****更新*****

            double height = srcImage.Height;
            double width = srcImage.Width;
            newWidth = (int)(srcImage.Width);
            double aspect = scale / width;
            newHeight = Convert.ToInt32(aspect * height);
            newWidth = Convert.ToInt32(aspect * width);
            Image scaledImage = ScaleDownTo(srcImage, newHeight, newWidth);
            newWidth = scaledImage.Width;
            newHeight = scaledImage.Height;
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            scaledImage.Save(ms, format);
            ms.Position = 0;
            _bytes = ms.ToArray();
            scaledImage.Dispose();

我实际做的是将图像调整为静态最大宽度,从任意到300px,所以我计算当前图像的宽度,将纵横比加倍,并将图像调整为此大小。

这方面的任何帮助都会得到

二进制图像大小调整出错

p>这里有一个缩小图像的简短函数。
    public static Image ScaleDownTo(Image image, int height, int width) {
        if (image != null) {
            if (image.Width > width || image.Height > height) {
                float factor = Math.Max(((float)width) / image.Width, ((float)height) / image.Height);
                if (factor > 0) {
                    RectangleF imgRect = new RectangleF(0, 0, image.Width * factor, image.Height * factor);
                    imgRect.X = ((float)width - imgRect.Width) / 2f;
                    imgRect.Y = ((float)height - imgRect.Height) / 2f;
                    Bitmap cellImage = new Bitmap(width, height);
                    using (Graphics cellImageGraphics = Graphics.FromImage(cellImage)) {
                        cellImageGraphics.Clear(Color.Transparent);
                        cellImageGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        cellImageGraphics.DrawImage(image, imgRect);
                    }
                    return cellImage;
                }
            }
            return image;
        }
        return null;
    }

只需使用它即可获得新的缩小图像。