我正在尝试将原始图像缩放到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;
我正在进行正确的缩放吗?我需要更改哪一个在使用上述功能进行缩放时获得较小尺寸的图像。
答案 0 :(得分:1)
我认为我们可以通过更改 PixelFormat 类型来缩小图片大小。
答案 1 :(得分: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[]));
}
}
}
}
}
}
这是结果:
编辑: 对于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;