C# 确保大于5 mb的图像缩小到该大小c以下#
我正在使用ImageProcessor来降低图像的分辨率或质量,但我不知道如何确保图像的最终大小小于5 MB。我尝试将图像尺寸设置为3840-2160,但我想使用更好的选项 这是我的代码:C# 确保大于5 mb的图像缩小到该大小c以下#,c#,image-processing,imageprocessor,C#,Image Processing,Imageprocessor,我正在使用ImageProcessor来降低图像的分辨率或质量,但我不知道如何确保图像的最终大小小于5 MB。我尝试将图像尺寸设置为3840-2160,但我想使用更好的选项 这是我的代码: private static byte[] redimensionImage(ref byte[] photoBytes) { var byteCuantity = ConvertBytesToMegabytes(photoBytes.Count()); ISuppor
private static byte[] redimensionImage(ref byte[] photoBytes)
{
var byteCuantity = ConvertBytesToMegabytes(photoBytes.Count());
ISupportedImageFormat format = new JpegFormat();
using (MemoryStream inStream = new MemoryStream(photoBytes))
{
using (MemoryStream outStream = new MemoryStream())
{
// Initialize the ImageFactory using the overload to preserve EXIF metadata.
using (ImageFactory imageFactory = new ImageFactory(preserveExifData: true))
{
// Load, resize, set the format and quality and save an image.
using (var imageProcessor = imageFactory.Load(inStream))
{
var originalHeight = imageProcessor.Image.Size.Height;
var originalWidth = imageProcessor.Image.Size.Width;
//calculate aspect ratio
var aspect = originalWidth / (float)originalHeight;
int newWidth, newHeight;
var dimenssionTooSmall = false;
if (originalWidth <= originalHeight && originalWidth < 100)
{
//calculate new dimensions based on aspect ratio
newHeight = (int)(100 / aspect);
var resizeLayer = new ResizeLayer(new Size(100, newHeight), ResizeMode.Min);
imageProcessor.Resize(resizeLayer);
dimenssionTooSmall = true;
}
else if (originalHeight < originalWidth && originalHeight < 100)
{
//calculate new dimensions based on aspect ratio
newWidth = (int)(100 / aspect);
var resizeLayer = new ResizeLayer(new Size(newWidth, 100), ResizeMode.Min);
imageProcessor.Resize(resizeLayer);
dimenssionTooSmall = true;
}
if (byteCuantity > 1 || dimenssionTooSmall)
{
//format.Quality = 6;
imageProcessor.Resize(new ResizeLayer(new Size(3840, 2160), ResizeMode.Min));
imageProcessor.Format(format);
imageProcessor.Save(outStream);
return outStream.ToArray();
}
else
{
return inStream.ToArray();
}
}
}
}
}
}
private static byte[]redimensionImage(ref byte[]photoBytes)
{
var bytecuanty=ConvertBytesToMegabytes(photoBytes.Count());
iSupportDimageFormat=新的JPEG格式();
使用(MemoryStream inStream=新的MemoryStream(photoBytes))
{
使用(MemoryStream outStream=新MemoryStream())
{
//使用重载初始化ImageFactory以保留EXIF元数据。
使用(ImageFactory ImageFactory=new ImageFactory(preserveExifData:true))
{
//加载、调整大小、设置格式和质量并保存图像。
使用(var imageProcessor=imageFactory.Load(流内))
{
var originalHeight=imageProcessor.Image.Size.Height;
var originalWidth=imageProcessor.Image.Size.Width;
//计算纵横比
var aspect=原始宽度/(浮动)原始高度;
int newWidth,newHeight;
var DimensionToosall=假;
if(原始宽度1 | |尺寸过小)
{
//格式质量=6;
Resize(新ResizeLayer(新大小(38402160),ResizeMode.Min));
imageProcessor.Format(格式);
图像处理器。保存(扩展);
返回外流ToArray();
}
其他的
{
返回流内。ToArray();
}
}
}
}
}
}
感谢和问候。不幸的是,除非保存为位图,否则不重新处理就无法真正做到这一点 保存图像时,会进行许多压缩过程,以每种格式存储图像(位图除外,位图不压缩图像)。如果不实际执行该过程,就无法预测文件大小
您可以创建自己的查找表,通过调整不同格式图像的大样本大小并收集输出大小,为将来的处理提供粗略估计,以此作为指导 你能得到转换后字节的大小吗?这会告诉你它有多大。是的,我可以,但如果可能的话,我想避免再加工,直到我得到正确的尺寸。