博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
步步为营-71-asp.net的简单练习(图片处理)
阅读量:4672 次
发布时间:2019-06-09

本文共 5428 字,大约阅读时间需要 18 分钟。

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页面

html
using System;using System.Collections.Generic;using System.Drawing;using System.Linq;using System.Web;namespace _06_图片处理{    ///     /// AddLogo 的摘要说明    ///     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(" ");        }        public bool IsReusable        {            get            {                return false;            }        }    }}
AddLogo.ashx

1.3 运行效果

2 验证码

2.1 ashx页面

using System;using System.Collections.Generic;using System.Drawing;using System.Linq;using System.Web;using System.Drawing.Imaging;namespace _06_图片处理{    ///     /// ValidateCode 的摘要说明    ///     public class ValidateCode : IHttpHandler    {        public void ProcessRequest(HttpContext context)        {            //01 验证码是图片,所以修改Type            context.Response.ContentType = "image/jpeg";            //02 创建画布            Bitmap bm = new Bitmap(70,30);                                  //03 创建绘图工具            Graphics g =   Graphics.FromImage(bm);            //03-01 设置背景色            g.Clear(Color.Green);            //04 准备绘制            string strArry = "abcdefghijklmnopqrstuvwxyz0123456789";            string vCode = string.Empty;            Random r = new Random ();            for (int i = 0; i < 4; i++)            {                vCode += strArry[r.Next(strArry.Length)];            }            //05 开始绘制            g.DrawString(vCode,new Font (new FontFamily("宋体"),20),new SolidBrush(Color.Red),0,0);            //06 保存            bm.Save(context.Response.OutputStream,ImageFormat.Jpeg);            //context.Response.Write("Hello World");        }        public bool IsReusable        {            get            {                return false;            }        }    }}
ashx

2.2 HTML页面

看不清,换一张
html

2.3 效果图

3 缩略图

3.1 HTML页面和ashx代码

html
using System;using System.Collections.Generic;using System.Drawing;using System.Linq;using System.Web;namespace _06_图片处理{    ///     /// SmallImag 的摘要说明    ///     public class SmallImag : IHttpHandler    {        public void ProcessRequest(HttpContext context)        {            context.Response.ContentType = "text/html";            //01 获取上传对象            HttpPostedFile hf = context.Request.Files["OrImg"];            //01-01 获取文件名称和后缀名            string name = hf.FileName;            string extName = hf.FileName.Substring(hf.FileName.IndexOf('.'));            //01-02 获取文件路径 和 相对路径            string path = FileHelper.GetFilePath();            string showPath = path.Substring(path.IndexOf("Upload"));            //02 保存原图片            hf.SaveAs(path+extName);            #region 03 绘制缩略后的小图            //03-00 规定缩放比例            float scale = 0.3f;            //03-01 获取原图片            Bitmap bmBig = new Bitmap(hf.InputStream);            //03-02 创建画布            Bitmap bm = new Bitmap((int)(bmBig.Width * scale),(int)(bmBig.Height * scale));            //03-03 获取绘制工具            Graphics g = Graphics.FromImage(bm);            //03-04 开始绘制            g.DrawImage(bmBig, 0, 0, (bmBig.Width * scale), (bmBig.Height * scale));                      #endregion            //04 保存缩略图            bm.Save(path +"_small"+ extName);            //05 展示缩略图            context.Response.Write("");        }        public bool IsReusable        {            get            {                return false;            }        }    }}
SmallImag.ashx

3.2 效果图

转载于:https://www.cnblogs.com/YK2012/p/7011291.html

你可能感兴趣的文章
php 创建简单的Restful WebAPI(三)
查看>>
C#遍历DataSet中数据的几种方法总结
查看>>
linux tomcat安装以及配置
查看>>
Git——Git的简单介绍【一】
查看>>
Vue源码学习三 ———— Vue构造函数包装
查看>>
winform编程中的跨线程访问资源(转)
查看>>
自制操作系统Antz(5)——深入理解保护模式与进入方法
查看>>
Creating one array of strings in c fails ,why?
查看>>
POJ 3683 Priest John's Busiest Day(2-sa路径输出,4级)
查看>>
hdu 1244 Max Sum Plus Plus Plus(DP线性区间)
查看>>
4.unity3D 预设的一例
查看>>
XP Sp3 开机就要激活,否则无法登录windows桌面
查看>>
转:智能模糊测试工具 Winafl 的使用与分析
查看>>
初识 Fuzzing 工具 WinAFL
查看>>
python:学习自顶向下程序设计:竞技体育模拟
查看>>
整数中1出现的次数(important)
查看>>
【转】软件设计模式六大原则详解
查看>>
线程中允许操作控件
查看>>
简单的论坛信息
查看>>
知识点:Oracle+表连接方式(内连接-外连接-自连接)+详解 来自百度文库
查看>>