C#生成高清缩略图

/// <SUMMARY>

  /// 为图片生成缩略图

  /// </SUMMARY>

  /// <PARAM name="phyPath">原图片的路径</PARAM>

  /// <PARAM name="width">缩略图宽</PARAM>

  /// <PARAM name="height">缩略图高</PARAM>

  /// <RETURNS></RETURNS>

  public System.Drawing.Image GetThumbnail(System.Drawing.Image image, int width, int height)

  {

  Bitmap bmp = new Bitmap(width, height);

  //从Bitmap创建一个System.Drawing.Graphics

  System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);

  //设置

  gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

  //下面这个也设成高质量

  gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

  //下面这个设成High

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

  //把原始图像绘制成上面所设置宽高的缩小图

  System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, width, height);

  gr.DrawImage(image, rectDestination, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);

  return bmp;

  }

  调用方法

  HttpPostedFile file = photoFile.PostedFile;

  if (!file.ContentType.Contains("image"))

  {

  return "照片格式不合法";

  }

  string ext = Path.GetExtension(file.FileName)。ToLower();

  if (ext != ".jpg" && ext != ".gif" && ext != ".png" && ext != ".jpeg")

  {

  return "请您上传jpg、gif、png图片";

  }

  if (file.ContentLength > 5 * 1024 * 1024)

  {

  return "请您上传512字节内的图片";

  }

  string newName = Guid.NewGuid()。ToString();

  string tempPath = "upload/";

  string img = tempPath + newName + ext;

  string filePath = Server.MapPath(img);

  if (!Directory.Exists(tempPath))

  {

  Directory.CreateDirectory(tempPath);

  }

  using (System.Drawing.Image originalImage = System.Drawing.Image.FromStream(file.InputStream))

  {

  GetThumbnail(originalImage, 504, 374)。Save(filePath);

  }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值