二进制图像大小调整出错

时间:2015-07-02 09:08:23

标签: c# image-processing binary memorystream

如果需要,我会尝试将二进制图像调整为较小的文件,但是调整大小后所有图像都会变得更多,而尺寸越来越小,因此图像非常难看......不知道为什么它会变大。

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

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,所以我计算当前图像宽度,将方面取两倍并将此图像调整为此大小。

对此的任何帮助都非常适合

1 个答案:

答案 0 :(得分:0)

嗯,这是缩小图像的简短功能。

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

只需使用它来获取新的缩小图像。