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