码迷,mamicode.com
首页 > Windows程序 > 详细

C# 通过ASHX保存上传的图片并制作高质量的缩略图的代码

时间:2019-01-16 11:54:01      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:ashx   tostring   response   load   保存   tip   表单   通过   http   

如下的内容段是关于C# 通过ASHX保存上传的图片并制作高质量的缩略图的内容,应该能对小伙伴也有帮助。

<%@ WebHandler Language="C#" Class="UploadFile" Debug="true" %>

using System;
using System.Web;

public class UploadFile : IHttpHandler
{

  public void Proce***equest(HttpContext context)
  {
    context.Response.ContentType = "text/plain";
    HttpPostedFile f1 = context.Request.Files["f1"];
    String fileExt = System.IO.Path.GetExtension(f1.FileName);
    System.Drawing.Image image = System.Drawing.Image.FromStream(f1.InputStream);
    int newWidth = 300, newHeight = 200;
    if ((decimal)image.Width / image.Height > (decimal)newWidth / newHeight)
    {
    }
    else if ((decimal)image.Width / image.Height < (decimal)newWidth / newHeight)
    {
    }
    System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(newWidth, newHeight);
    System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp);
    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
    System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, newWidth, newHeight);
    g.DrawImage(image, rectDestination, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);
    bmp.Save(context.Server.MapPath("~/") + DateTime.Now.ToString("yyyyMMddHHmmss") + fileExt);
    bmp.Dispose();
    image.Dispose();
    context.Response.Write("OK");
  }

  public bool IsReusable
  {
    get
    {
      return false;
    }
  }

}

上传表单

<form id="form1" action="UploadFile.ashx" method="post" enctype="multipart/form-data">
<input type="file" name="f1" />
<input type="submit" />
</form>

C# 通过ASHX保存上传的图片并制作高质量的缩略图的代码

标签:ashx   tostring   response   load   保存   tip   表单   通过   http   

原文地址:http://blog.51cto.com/14139747/2343218

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!