教程集 www.jiaochengji.com
教程集 >  脚本编程  >  Asp.net  >  正文 c#生成缩略图不失真的简单示例

c#生成缩略图不失真的简单示例

发布时间:2016-07-10   编辑:jiaochengji.com
分享一例c#生成不失真的缩略图的代码,学习下c# 缩略图的生成方法,以及高质量缩略图的处理技巧,有需要的朋友参考下。

平时使用.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#生成缩略图不失真的简单示例
php生成缩略图的例子
给图片生成缩略图和加版权的类
php生成缩略图自动填充白边例子
PHP原生写的生成图片缩略图类
ecms6.6等比例生成缩略图
c#(asp.net)图片上传且生成高清缩略图的代码
php图片文件上传类(可自动生成缩略图)
php缩略图填充白边的示例代码
PHP图片处理类 phpThumb参数用法详解

关键词: 缩略图   
[关闭]
~ ~