2015-12-16 53 views
1

我试图将原始图像缩放为50%和25%,并尝试在MVC中下载缩放图像。我正在使用从Google搜索中获取的以下代码。在缩放时获取更大尺寸的图像C#

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的图像返回4.7MB的50%,甚至100%的最差返回18 MB。

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

 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; 

我是否正确地进行缩放?哪一个我需要改变获得较小的图像,同时使用上述功能进行缩放。

+2

你是比较两个字节数组的大小还是磁盘上文件的大小?请记住,当你加载一个500Kb的JPG文件时,它会在内存中解压缩,产生一个更大的字节数组(至少32bits *像素宽×像素高)。因此,您可以从一个非常小的JPG文件中获得“几兆字节”的字节数组(这可能是您在100%调整大小时看到的情况)。这将有效地匹配您的值:百分比50%=结果图像是原始图像的1/4,4.7Mb是18Mb的1/4。 –

+0

@Leo。我正在比较磁盘上文件的大小。请参阅我的编辑。 – Ramesh

+0

我为你发布了一个答案。正如我所说你的代码完美地工作,这是一个图像压缩的问题。试试看,请让我知道! –

回答

1

你的代码的工作,我相信这只是图像压缩的问题,基本上你推你的字节数组的输出流不变,同时应将其保存为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[])); 
       } 
      } 
     } 
    } 
} 
} 

这是结果:

enter image description here

编辑: 好了的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;