用户名:
密 码: 记住
您当前的位置:首页 > 网络编程 > Net教程

c#生成缩略图不失真的方法实例分享

时间:2015-01-29  来源:互联网  作者:佚名

复制代码 代码如下:
/// <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;
}
}

来顶一下
返回首页
返回首页
推荐资讯
asp.net中页面显示当前系统时间附图 asp.net中页面显示当前系统时间附图复制代码 代码如下: <p class="time">当前系统时间:<scri
asp.net用Zxing库实现条形码输出的具体实现 asp.net用Zxing库实现条形码输出的这个简单的问题,困扰了我将近两个小时,主要是再官方文档上
相关文章
栏目更新
栏目热门