展会信息港展会大全

c#生成缩略图不失真的方法
来源:互联网   发布日期:2016-01-26 10:54:51   浏览:1627次  

导读:使用 net的方法GetThumbnailImage生成的缩略图失真严重,这里推荐一种不失真生成缩略图的方法代码如下: summary 获得缩微图 summary returns returns public bool GetThumbImg(){tr ...

使用.net的方法GetThumbnailImage生成的缩略图失真严重,这里推荐一种不失真生成缩略图的方法

代码如下:

/// <summary>

/// 获得缩微图

/// </summary>

/// <returns></returns>

public bool GetThumbImg()

{

try

{

string imgpath; //原始路径

if(imgsourceurl.IndexOf("",0)<0) //使用的是相对路径

{

imgpath = HttpContext.Current.Server.MapPath(imgsourceurl); //转化为物理路径

}

else

{

imgpath=imgsourceurl;

}

System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(imgpath);

int width = sourceImage.Width;

int height = sourceImage.Height;

if(thumbwidth <= 0)

{

thumbwidth = 120;

}

if(thumbwidth >= width)

{

return false;

}

else

{

(thumbwidth,thHeight*thumbwidth/thWidth,null,IntPtr.Zero);

Image imgThumb=new System.Drawing.Bitmap(thumbwidth,height*thumbwidth/width);

System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(imgThumb);

g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

g.DrawImage(sourceImage, new Rectangle(0, 0, thumbwidth,height*thumbwidth/width), 0, 0, width, height, GraphicsUnit.Pixel);

string thumbpath="";

sourceImage.Dispose();

if(thumburl=="")

{

thumbpath=imgpath;

}

if(thumbpath.IndexOf("",0)<0)//使用的是相对路径

{

thumbpath=HttpContext.Current.Server.MapPath(thumburl);//转化为物理路径

}

imgThumb.Save(thumbpath,ImageFormat.Jpeg);

imgThumb.Dispose();

return true;

}

}

catch

{

throw;

}

}

赞助本站

人工智能实验室

相关热词: 生成缩略图 失真 c

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港