0
我无法明白为什么此代码会创建之前1280x800的图像的缩略图,尺寸为241kb至600x375,大小为556kb。下面是代码:C# - 在创建相同图像的较小分辨率后图像尺寸增加
using (System.Drawing.Image img = System.Drawing.Image.FromFile(@"c:\images\myImg.jpg"))
{
int sourceWidth = img.Width;
int sourceHeight = img.Height;
int thumbHeight, thumbWidth = 0;
decimal ratio = decimal.Divide(sourceHeight, sourceWidth);
if (sourceHeight > 600 || sourceWidth > 800)
{
if (ratio >= 1) // Image is higher than it is wide.
{
thumbHeight = 800;
thumbWidth = Convert.ToInt32(decimal.Divide(sourceWidth, sourceHeight) * thumbHeight);
}
else // Image is wider than it is high.
{
thumbWidth = 600;
thumbHeight = Convert.ToInt32(decimal.Divide(sourceHeight, sourceWidth) * thumbWidth);
}
using (Bitmap bMap = new Bitmap(thumbWidth, thumbHeight))
{
Graphics gr = Graphics.FromImage(bMap);
gr.SmoothingMode = SmoothingMode.HighQuality;
gr.CompositingQuality = CompositingQuality.HighQuality;
gr.InterpolationMode = InterpolationMode.High;
Rectangle rectDestination = new Rectangle(0, 0, thumbWidth, thumbHeight);
gr.DrawImage(img, rectDestination, 0, 0, sourceWidth, sourceHeight, GraphicsUnit.Pixel);
bMap.Save(HttpContext.Current.Server.MapPath("~/i/" + filename + "_" + fileExtension));
}
}
}
任何帮助将不胜感激。 谢谢, 本
更改*尺寸*与改变图像的*分辨率*不同。 您所做的只是缩小图像尺寸,导致更多的像素被打包到更小的空间中。 – 2011-12-18 06:15:16
很可能,输入图像的压缩质量较低,而输出图像的压缩质量较高。 – Rotem 2011-12-18 06:29:12