[英]Why resizing causes image size to decrease
我繼承了一個可以在寬度和高度上調整圖像大小的代碼。 我還觀察到的是,即使圖像的高度和寬度增加,它也會減小文件大小。 這是保存圖像的調整大小代碼和調用函數代碼
例如,這可能會更好地解釋。我的原始圖像為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;
}
我認為並且可能會遇到縱橫比不高的問題,但是是否建議保持原樣?或者在圖像高度和寬度增加時理想地增加文件大小是否合乎邏輯?
對於這個問題,我想在高度和寬度增加時增加文件大小,但是歡迎最佳實踐建議。
有什么建議么?
顯然,這是保存為與原始格式不同的圖像格式(bmp或png與jpg)或為JPG使用不同的壓縮強度的情況。
加載圖像時應注意圖像的格式,然后以相同格式保存回去。 或在保存jpg時確保使用不同的壓縮強度。 在這里查看更多。
您正在將調整大小后的圖像保存為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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.