日期:2014-05-18  浏览次数:20885 次

求图片放大缩小但图片保持清晰的方法!!
求图片放大缩小但图片保持清晰的方法!!!


------解决方案--------------------
public void reDrawSaveImage(System.IO.Stream imgStream,string savePath,int upWidth,int upHeight)
{
int oldWidth;
int oldHeight;
int newWidth;
int newHeight;

newWidth = upWidth;
newHeight = upHeight;

System.Drawing.Image oldImg = System.Drawing.Image.FromStream(imgStream);

oldWidth = oldImg.Width;
oldHeight = oldImg.Height;

if(oldWidth > = oldHeight)
{
newHeight = (int)Math.Floor(Convert.ToDouble(oldHeight) * Convert.ToDouble(newWidth) / Convert.ToDouble(oldWidth));
}
else
{
newWidth = (int)Math.Floor(Convert.ToDouble(oldWidth) * Convert.ToDouble(newHeight) / Convert.ToDouble(oldHeight));
}

Bitmap newImg = new Bitmap(newWidth,newHeight);
Graphics g = Graphics.FromImage(newImg);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量插值法
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度
g.Clear(Color.Transparent); //清空画布并以透明背景色填充
g.DrawImage(oldImg,new Rectangle(0,0,newWidth,newHeight),new Rectangle(0,0,oldWidth,oldHeight),GraphicsUnit.Pixel);
newImg.Save(savePath,System.Drawing.Imaging.ImageFormat.Jpeg);

g.Dispose();
oldImg.Dispose();
newImg.Dispose();

}