源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2022-08-17 05:07 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:c#生成缩略图不失真的方法实例分享
[u]复制代码[/u] 代码如下:
/// <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; } }
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部