我正在尝试调整图片大小并使用以下代码段保存。它工作正常但有些图像在调整大小后会失去质量。当我检查时,原始图像看起来很好,只有重新调整大小的图像质量很差。我不知道如何在调整大小的同时提高图像质量。
System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, MaxHeight, null, IntPtr.Zero);
// Clear handle to original file so that we can overwrite it if necessary
FullsizeImage.Dispose();
// Save resized picture
//NewImage.Save(NewFile);
if (fileExtension.ToLower() == ".jpg" || fileExtension.ToLower() == ".jpeg")
{
NewImage.Save(NewFile, System.Drawing.Imaging.ImageFormat.Jpeg);
}
请帮帮我。感谢。
答案 0 :(得分:2)
您可以使用此课程:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Drawing.Drawing2D;
/// <summary>
/// Summary description for ResizeImage
/// </summary>
public class ResizeImage
{
public static Image Resize(Image imgToResize, int h, int w)
{
Size size = new Size(w, h);
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 destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();
return (Image)b;
}
}
此外,您可以使用此代码来选择图像质量:
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphic.SmoothingMode = SmoothingMode.HighQuality;
graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphic.CompositingQuality = CompositingQuality.HighQuality;
赞here。
答案 1 :(得分:0)
随着图像大小调整,你还应该记住更多的事情(经验法则,而不是福音,因为它取决于你正在做什么等等)......
希望这有帮助,