溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C# 通過(guò)ASHX保存上傳的圖片并制作高質(zhì)量的縮略圖的代碼

發(fā)布時(shí)間:2020-06-27 05:08:16 來(lái)源:網(wǎng)絡(luò) 閱讀:785 作者:yellowred 欄目:編程語(yǔ)言

如下的內(nèi)容段是關(guān)于C# 通過(guò)ASHX保存上傳的圖片并制作高質(zhì)量的縮略圖的內(nèi)容,應(yīng)該能對(duì)小伙伴也有幫助。

<%@ 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>
向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI