加入收藏 | 设为首页 | 会员中心 | 我要投稿 晋中站长网 (https://www.0354zz.com/)- 科技、容器安全、数据加密、云日志、云数据迁移!
当前位置: 首页 > 综合聚焦 > 编程要点 > 语言 > 正文

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

发布时间:2023-06-09 16:00:11 所属栏目:语言 来源:互联网
导读:   代码如下:



  /// <summary>



  /// 获得缩微图



  /// </summary>



  /// <returns></returns>



   public bool GetThumbImg()



  {

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

(编辑:晋中站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章