缩放 C# 时获取更大尺寸的图像

Getting bigger size image while scaling C#

我正在尝试将原始图像缩放到 50% 和 25%,并尝试在 MVC 中下载缩放后的图像。我正在使用从 Google 搜索中获取的以下代码。

public byte[] ScaleImageByPercent(byte[] imageBuffer, int Percent)
    {

        using (Stream imageStream = new MemoryStream(imageBuffer))
        {
            using (Image scaleImage = Image.FromStream(imageStream))
            {
                float scalePercent = ((float)Percent / 100);

                int originalWidth = scaleImage.Width;
                int originalHeight = scaleImage.Height;
                int originalXPoint = 0;
                int originalYPoint = 0;

                int scaleXPoint = 0;
                int scaleYPoint = 0;
                int scaleWidth = (int)(originalWidth * scalePercent);
                int scaleHeight = (int)(originalHeight * scalePercent);

                using (Bitmap scaleBitmapImage = new Bitmap(scaleWidth, scaleHeight, PixelFormat.Format24bppRgb))
                {
                    scaleBitmapImage.SetResolution(scaleImage.HorizontalResolution, scaleImage.VerticalResolution);
                    Graphics graphicImage = Graphics.FromImage(scaleBitmapImage);
                    graphicImage.CompositingMode = CompositingMode.SourceCopy;
                    graphicImage.InterpolationMode = InterpolationMode.NearestNeighbor;
                    graphicImage.DrawImage(scaleImage,
                        new Rectangle(scaleXPoint, scaleYPoint, scaleWidth, scaleHeight),
                        new Rectangle(originalXPoint, originalYPoint, originalWidth, originalHeight),
                        GraphicsUnit.Pixel);
                    graphicImage.Dispose();

                    ImageConverter converter = new ImageConverter();
                    return (byte[])converter.ConvertTo(scaleBitmapImage, typeof(byte[]));
                }
            }
        }
    }

当我使用 3.4MB 图像时,它在 50% 时返回 4.7MB,甚至在 100% 时最差返回 18MB。

编辑: 获取字节数组后,我正在使用以下代码下载图像。下载后,我检查磁盘中的文件大小,它显示更大的大小。

        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
        result.Content = new StreamContent(new MemoryStream(scaledBytes));
        result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
        result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
        return result;

我缩放比例正确吗?在使用上述功能进行缩放时,我需要更改哪个图像以获得较小尺寸的图像。

我认为我们可以通过更改 PixelFormat 类型来减小图像的大小。

你可以参考这个Reducing Bitmap bit-size in C#

您的代码有效,我相信这只是图像压缩的问题,基本上您是将字节数组按原样推送到输出流,同时您应该将其保存为 jpeg。在我的示例中,为简单起见,我使用 FileStream,在您的情况下,您应该使用输出流。 试一试(只需将任何 Jpg 文件拖放到已编译的可执行文件上):

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            string filePath = System.IO.Path.GetFullPath(args[0]);
            byte[] originalImage = System.IO.File.ReadAllBytes(filePath);
            byte[] resizedImage = ScaleImageByPercent(originalImage, 50);
            using (Stream imageStream = new MemoryStream(resizedImage))
            {
                using (Image scaleImage = Image.FromStream(imageStream))
                {
                    string outputPath = System.IO.Path.GetDirectoryName(filePath);
                outputPath = System.IO.Path.Combine(outputPath, $"{System.IO.Path.GetFileNameWithoutExtension(filePath)}_resized.jpg");
                using (FileStream outputFile = System.IO.File.Open(outputPath, FileMode.Create, FileAccess.Write))
                {
                    scaleImage.Save(outputFile, ImageFormat.Jpeg);
                }
            }
        }
    }
    public static byte[] ScaleImageByPercent(byte[] imageBuffer, int Percent)
    {

        using (Stream imageStream = new MemoryStream(imageBuffer))
        {
            using (Image scaleImage = Image.FromStream(imageStream))
            {
                float scalePercent = ((float)Percent / 100);

                int originalWidth = scaleImage.Width;
                int originalHeight = scaleImage.Height;
                int originalXPoint = 0;
                int originalYPoint = 0;

                int scaleXPoint = 0;
                int scaleYPoint = 0;
                int scaleWidth = (int)(originalWidth * scalePercent);
                int scaleHeight = (int)(originalHeight * scalePercent);

                using (Bitmap scaleBitmapImage = new Bitmap(scaleWidth, scaleHeight, PixelFormat.Format24bppRgb))
                {
                    scaleBitmapImage.SetResolution(scaleImage.HorizontalResolution, scaleImage.VerticalResolution);
                    Graphics graphicImage = Graphics.FromImage(scaleBitmapImage);
                    graphicImage.CompositingMode = CompositingMode.SourceCopy;
                    graphicImage.InterpolationMode = InterpolationMode.NearestNeighbor;
                    graphicImage.DrawImage(scaleImage,
                        new Rectangle(scaleXPoint, scaleYPoint, scaleWidth, scaleHeight),
                        new Rectangle(originalXPoint, originalYPoint, originalWidth, originalHeight),
                        GraphicsUnit.Pixel);
                    graphicImage.Dispose();

                    ImageConverter converter = new ImageConverter();
                    return (byte[])converter.ConvertTo(scaleBitmapImage, typeof(byte[]));
                }
            }
        }
    }
}
}

这是结果:

编辑: 好的,对于 webapi 接口尝试这样做:

            HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);

        using (Stream imageStream = new MemoryStream(resizedImage))
        {
            using (Image scaleImage = Image.FromStream(imageStream))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    scaleImage.Save(ms, ImageFormat.Jpeg);
                    result.Content = new StreamContent(ms);
                    result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
                    result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
                }
            }
        }
        return result;