我继承了一个可以在宽度和高度上调整图像大小的代码。我还观察到的是,即使图像的高度和宽度增加,它也会减小文件大小。这是保存图像的调整大小代码和调用函数代码
例如,这可能会更好地解释。我的原始图像为709 * 653像素,文件大小为670 kb
调整大小后,应为1000 * 921,但预期大小为176 kb
Image orgImage = Image.FromFile(originalimagepath);
//resizes image using below function
transformedImage = ImageUtils.resizeImage(orgImage, Settings.MaxLargeImage);
memstrImage = new MemoryStream();
//saves image to memorystream which is in turn saved in destination as resized image.
transformedImage.Save(memstrImage,ImageFormat.Jpeg);
public static Image resizeImage(Image imgToResize, int resizeType)
{
//resizetype is maximum resize width I want image to take.
int height = imgToResize.Height;
int width = imgToResize.Width;
float ratio = 0;
Size size = new Size();
if (height >= width)
{
ratio = ((float)resizeType / (float)height);
}
else
{
ratio = ((float)resizeType / (float)width);
}
size.Height = Convert.ToInt32((float)ratio * (float)imgToResize.Height);
size.Width = Convert.ToInt32((float)ratio * (float)imgToResize.Width);
int sourceWidth = imgToResize.Width;
int sourceHeight = imgToResize.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)size.Width / (float)sourceWidth);
nPercentH = ((float)size.Height / (float)sourceHeight);
if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;
int destHeight = Convert.ToInt32(sourceHeight * nPercent);
int destWidth = Convert.ToInt32(sourceWidth * nPercent);
Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((Image)b);
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();
return (Image)b;
}
我认为并且可能会遇到纵横比不高的问题,但是是否建议保持原样?或者在图像高度和宽度增加时理想地增加文件大小是否合乎逻辑?
对于这个问题,我想在高度和宽度增加时增加文件大小,但是欢迎最佳实践建议。
有什么建议?
您正在将调整大小后的图像保存为Jpeg格式。Jpeg是一种“有损”格式,这意味着,为了节省空间,所保存的图像仅是内存中位图的近似值,其选择方式是使它在人眼看来与原始图像“接近”。如果您只是Image.Save()
用来保存位图,那么根据这篇文章,“存储质量”被认为是75:Image.Save()对jpeg文件使用什么质量等级?。(无论如何Image.Save()
,Jpeg格式的质量都没有记录。)
因此,如果调整位图的大小并将其保存到Jpeg,则由于压缩,生成的文件可能小于原始文件,也可能大于原始文件,而与尺寸变化无关。(尽管尺寸的大变化最终将主导任何压缩问题,因为位图的原始尺寸随像素尺寸的缩放而平方增加。)
如果要尝试在保存JPG时质量设置如何影响文件大小,可以使用以下方法:
public static void SaveJpegWithSpecifiedQuality(this Image image, string filename, int quality)
{
// http://msdn.microsoft.com/en-us/library/ms533844%28v=vs.85%29.aspx
// A quality level of 0 corresponds to the greatest compression, and a quality level of 100 corresponds to the least compression.
if (quality < 0 || quality > 100)
{
throw new ArgumentOutOfRangeException("quality");
}
System.Drawing.Imaging.Encoder qualityEncoder = System.Drawing.Imaging.Encoder.Quality;
EncoderParameters encoderParams = new EncoderParameters(1);
EncoderParameter encoderParam = new EncoderParameter(qualityEncoder, (long)quality);
encoderParams.Param[0] = encoderParam;
image.Save(filename, GetEncoder(ImageFormat.Jpeg), encoderParams);
}
private static ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句