步步為營-71-asp.net的簡單練習(圖片處理)

來源:互聯網
上載者:User

標籤:saveas   tle   tip   add   bool   .net   doc   加浮水印   proc   

1 原有圖片添加浮水印

1.1 封裝一個類,用於擷取檔案路徑

using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Web;namespace _06_圖片處理{    public static class FileHelper    {        public static string GetFilePath()         {            //02 建立檔案儲存路徑            string savePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "Upload\\");            //02-01 根據日期建立檔案夾            DateTime dt = DateTime.Now;            savePath += dt.Year + "\\" + dt.Month + "\\" + dt.Day;            if (!Directory.Exists(savePath))            {                //建立檔案夾                Directory.CreateDirectory(savePath);            }            //02-02文 件名為目前時間                        //savePath += "\\" + dt.ToString().Replace(‘:‘, ‘-‘) + ".gif";            savePath += "\\" + dt.ToString().Replace(‘:‘, ‘-‘) ;            return savePath;        }     }}
FileHelper

1.2 html頁面和ashx頁面

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>    <title></title></head><body>    <form action="AddLogo.ashx" method="post" enctype="multipart/form-data">        <input type="file" name="OrImg" />        <input type="submit" value="添加浮水印" />    </form></body></html>
html
using System;using System.Collections.Generic;using System.Drawing;using System.Linq;using System.Web;namespace _06_圖片處理{    /// <summary>    /// AddLogo 的摘要說明    /// </summary>    public class AddLogo : IHttpHandler    {        public void ProcessRequest(HttpContext context)        {            context.Response.ContentType = "text/html";            //01 擷取上傳圖片            HttpPostedFile pf = context.Request.Files["OrImg"];                        #region 02 添加浮水印            //02-01 建立畫布            Bitmap bm = new Bitmap(pf.InputStream);            //02-02 建立繪圖工具            Graphics gs = Graphics.FromImage(bm);            //02-03 拿到logo圖片            Bitmap bmLogo = new Bitmap(AppDomain.CurrentDomain.BaseDirectory + "/images/LogoYK.GIF");            //02-04 開始繪製            gs.DrawImage(bmLogo,bm.Width-bmLogo.Width,bm.Height-bmLogo.Height,bmLogo.Width,bmLogo.Height);            #endregion                        #region 03 儲存            //03-01 擷取副檔名            string extName = pf.FileName.Substring(pf.FileName.LastIndexOf(‘.‘));            //03-02 擷取檔案路徑            string ph = FileHelper.GetFilePath();            string savePath = ph + extName;            //03-03 saveAs            bm.Save(savePath);            #endregion            //04 展示            context.Response.Write("<img src=‘" + savePath.Substring(savePath.IndexOf("Upload")) + "‘/> ");        }        public bool IsReusable        {            get            {                return false;            }        }    }}
AddLogo.ashx

1.3 運行效果

步步為營-71-asp.net的簡單練習(圖片處理)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.