zoukankan      html  css  js  c++  java
  • 添加水印

    一.案例

    1. 上传文件;

    2. 为图片添加文字水印;

    3. 为图片添加图片水印;

    4. 生成缩略图;

    二.源代码

    Upload.html

     1 <!DOCTYPE html>
     2 <html xmlns="http://www.w3.org/1999/xhtml">
     3 <head>
     4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
     5     <title></title>
     6 </head>
     7 <body>
     8     <!-- 如果要上传,必须设置表单method=post,而且enctype="multipart/form-data"。 -->
     9     <!-- 一旦设置了enctype="multipart/form-data",那么浏览器生成请求报文的时候,就会生成数据分隔符。 -->
    10     <!-- 并且更换请求报文体的数据组织格式(使用分隔符来分开不同html表单控件的内容)。 -->
    11     <form method="post" action="Upload.ashx" enctype="multipart/form-data">
    12         <input type="file" name="file01" />
    13         <input type="file" name="file02" />        
    14         <input type="submit" value="上传" />
    15     </form>
    16 </body>
    17 </html>
    View Code

    Upload.ashx

      1 <%@ WebHandler Language="C#" Class="Upload" %>
      2 
      3 using System;
      4 using System.Web;
      5 using System.Drawing;
      6 using System.Text;
      7 
      8 public class Upload : IHttpHandler
      9 {
     10 
     11     public void ProcessRequest(HttpContext context)
     12     {
     13         ////一.上传文件
     14         ////1. 遍历所有上传来的文件
     15         //HttpPostedFile file = context.Request.Files[0];
     16         ////2. 判断文件大小
     17         //if (file.ContentLength > 0)
     18         //{
     19         //    //3. 获取文件名
     20         //    string strName = System.IO.Path.GetFileName(file.FileName);
     21         //    //4. 保存文件
     22         //    file.SaveAs(context.Server.MapPath("/upload/" + strName));
     23         //}
     24 
     25         ////二.为图片加文字水印                
     26         //StringBuilder sb = new StringBuilder();
     27         ////1. 遍历所有上传来的文件
     28         //for (int i = 0; i < context.Request.Files.Count; i++)
     29         //{
     30         //    HttpPostedFile file = context.Request.Files[i];
     31         //    //2. 判断文件大小
     32         //    if (file.ContentLength > 0)
     33         //    {
     34         //        //判断文件是否是文件
     35         //        if (file.ContentType.Contains("image/"))
     36         //        {
     37         //            using (Image img = Image.FromStream(file.InputStream))
     38         //            {
     39         //                using (Graphics g = Graphics.FromImage(img))
     40         //                {
     41         //                    g.DrawString("打撒比", new Font("微软雅黑", 14), Brushes.Red, 0, 0);
     42         //                }
     43 
     44         //                string strName = System.IO.Path.GetFileName(file.FileName);
     45         //                string phyPath = context.Server.MapPath("/upload/" + strName);
     46         //                img.Save(phyPath);
     47         //                sb.AppendLine(strName + "</br>");
     48         //            }
     49         //        }
     50         //    }
     51         //}
     52         //context.Response.Write("保存成功!</br>" + sb.ToString());
     53 
     54         ////三.为图片加图片水印                
     55         //StringBuilder sb = new StringBuilder();
     56         ////1. 遍历所有上传来的文件
     57         //for (int i = 0; i < context.Request.Files.Count; i++)
     58         //{
     59         //    HttpPostedFile file = context.Request.Files[i];
     60         //    //2. 判断文件大小
     61         //    if (file.ContentLength > 0)
     62         //    {
     63         //        //判断文件是否是文件
     64         //        if (file.ContentType.Contains("image/"))
     65         //        {
     66         //            //获取图片的文件流
     67         //            using (Image img = Image.FromStream(file.InputStream))
     68         //            {
     69         //                //获取水印图片的文件流
     70         //                using (Image imgWater = Image.FromFile(context.Server.MapPath("/img/头像.jpg")))
     71         //                {
     72         //                    using (Graphics g = Graphics.FromImage(img))
     73         //                    {
     74         //                        g.DrawImage(imgWater, 0, 0);
     75         //                    }
     76         //                }
     77 
     78         //                string strName = System.IO.Path.GetFileName(file.FileName);
     79         //                string phyPath = context.Server.MapPath("/upload/" + strName);
     80         //                img.Save(phyPath);
     81         //                sb.AppendLine(strName + "</br>");
     82         //            }
     83         //        }
     84         //    }
     85         //}
     86         //context.Response.Write("保存成功!</br>" + sb.ToString());
     87 
     88         //四.保存缩略图和原图               
     89         StringBuilder sb = new StringBuilder();
     90         //1. 遍历所有上传来的文件
     91         for (int i = 0; i < context.Request.Files.Count; i++)
     92         {
     93             HttpPostedFile file = context.Request.Files[i];
     94             //2. 判断文件大小
     95             if (file.ContentLength > 0)
     96             {
     97                 //判断文件是否是文件
     98                 if (file.ContentType.Contains("image/"))
     99                 {
    100                     //获取图片的文件流
    101                     using (Image img = Image.FromStream(file.InputStream))
    102                     {
    103                         string strImgName = file.FileName;
    104                         string strThumbName = "";
    105                         GetRandomName(ref strImgName, ref strThumbName);
    106                         //生成缩略图
    107                         using (Image imgThumb = new Bitmap(200, 150))
    108                         {
    109                             using (Graphics g = Graphics.FromImage(imgThumb))
    110                             {
    111                                 //参数1:原图
    112                                 //参数2:要把原图缩成多大
    113                                 //参数3:取原图的哪部分来缩略
    114                                 //参数4:单位(像素)
    115                                 g.DrawImage(img, new Rectangle(0, 0, imgThumb.Width, imgThumb.Height), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
    116                             }
    117                             string phyPathImg = context.Request.MapPath("/upload/" + strImgName);
    118                             string phyPathThumb = context.Request.MapPath("/upload/" + strThumbName);
    119                             img.Save(phyPathImg);
    120                             imgThumb.Save(phyPathThumb);
    121                             sb.AppendLine(strImgName + "</br>");
    122                             sb.AppendLine(strThumbName + "</br>");
    123                         } 
    124                     }
    125                 }
    126             }
    127         }
    128         context.Response.Write("保存成功!</br>" + sb.ToString());
    129     }
    130 
    131     /// <summary>
    132     /// 获取随机的文件名
    133     /// </summary>
    134     /// <returns></returns>
    135     string GetRandomName(string fileOrlName)
    136     {
    137         string orgName = System.IO.Path.GetExtension(fileOrlName);
    138         return Guid.NewGuid().ToString() + orgName;
    139     }
    140     /// <summary>
    141     /// 获取随机的文件名(区分原图和缩略图)
    142     /// </summary>
    143     /// <param name="imgName"></param>
    144     /// <param name="thumbName"></param>
    145     void GetRandomName(ref string imgName, ref string thumbName)
    146     {
    147         string fileName = Guid.NewGuid().ToString();
    148         string extention = System.IO.Path.GetExtension(imgName);
    149         //原图名字
    150         imgName = fileName + extention;
    151         //缩略图名字
    152         thumbName = fileName + "-thm" + extention;
    153     }
    154 
    155     public bool IsReusable
    156     {
    157         get
    158         {
    159             return false;
    160         }
    161     }
    162 
    163 }
    View Code
  • 相关阅读:
    selenium模拟浏览器进行百度搜索
    爬虫出现Forbidden by robots.txt
    Scrapy和Headless Chrome采集动态网站数据
    爬虫框架Scrapy的安装与基本使用
    Python Selenium库的使用
    拦截器HandlerInterceptorAdapter使用方法
    Python多进程并发(multiprocessing)
    Oracle自定义DES3加解密
    Springboot手动添加quartz-jobs
    python 发送邮件接口
  • 原文地址:https://www.cnblogs.com/HuoAA/p/4087858.html
Copyright © 2011-2022 走看看