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 运行效果

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_图片处理
{
/// <summary>
/// ValidateCode 的摘要说明
/// </summary>
public class ValidateCode : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
//01 验证码是图片,所以修改Type
context.Response.ContentType = "image/jpeg";
//02 创建画布
Bitmap bm = new Bitmap(,); //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 = ; i < ; i++)
{
vCode += strArry[r.Next(strArry.Length)];
}
//05 开始绘制
g.DrawString(vCode,new Font (new FontFamily("宋体"),),new SolidBrush(Color.Red),,);
//06 保存
bm.Save(context.Response.OutputStream,ImageFormat.Jpeg);
//context.Response.Write("Hello World");
} public bool IsReusable
{
get
{
return false;
}
}
}
}

ashx

2.2 HTML页面

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script src="Script/jquery-1.7.1.min.js"></script>
<title></title>
<script>
$(function () {
$("#changeCode").click(function () {
$('#validateCode').attr("src", $('#validateCode').attr("src")+'1');
});
})
</script>
</head>
<body>
<img id="validateCode" src="ValidateCode.ashx?1"/>
<a href="#" id="changeCode">看不清,换一张</a>
</body>
</html>

html

2.3 效果图

3 缩略图

3.1 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 method="post" action="SmallImag.ashx" 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>
/// SmallImag 的摘要说明
/// </summary>
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, , , (bmBig.Width * scale), (bmBig.Height * scale)); #endregion
//04 保存缩略图
bm.Save(path +"_small"+ extName);
//05 展示缩略图
context.Response.Write("<img src='"+showPath+"_small"+extName+"'/>");
} public bool IsReusable
{
get
{
return false;
}
}
}
}

SmallImag.ashx

3.2 效果图

步步为营-71-asp.net的简单练习(图片处理)的更多相关文章

  1. ASP.NET 实现简单的图片防盗链介绍

    在此,网站图片防盗链的方法是,通过获取Http请求头中的 Referer 标头与本网站域名比较,来判断用户是否来自本站跳转过来的 . 创建一个全局处理程序,用来处理images目录下的图片的直接请求: ...

  2. ASP.NET Core 简单实现七牛图片上传(FormData 和 Base64)

    ASP.NET Core 简单实现七牛图片上传(FormData 和 Base64) 七牛图片上传 SDK(.NET 版本):https://developer.qiniu.com/kodo/sdk/ ...

  3. 用Asp.net实现简单的文字水印

    用Asp.net实现简单的文字水印  经常看见MOP上有人贴那种动态的图片,就是把一个字符串作为参数传给一个动态网页,就会生成一个带有这个字符串的图片,这个叫做文字水印.像什么原来的熊猫系列,还有后来 ...

  4. 一般处理程序生成简单的图片验证码并通过html验证用户输入的验证码是否正确

    一般处理程序生成简单的图片验证码并通过html验证用户输入的验证码是否正确       最近没事研究了下验证码的的动态生成及通过cookie实现HTML页面对用户输入的验证码的校验,简要如下: 1.写 ...

  5. Asp.Net Core Web Api图片上传(一)集成MongoDB存储实例教程

    Asp.Net Core Web Api图片上传及MongoDB存储实例教程(一) 图片或者文件上传相信大家在开发中应该都会用到吧,有的时候还要对图片生成缩略图.那么如何在Asp.Net Core W ...

  6. Expression Blend4经验分享:制作一个简单的图片按钮样式

    这次分享如何做一个简单的图片按钮经验 在我的个人Silverlight网页上,有个Iphone手机的效果,其中用到大量的图片按钮 http://raimon.6.gwidc.com/Iphone/de ...

  7. [ASP.NET]更简单的方法:FormsAuthentication登录ReturnUrl使用绝对路径

    转自:http://www.cnblogs.com/dudu/p/formsauthentication-returnurl-absoluteuri.html [ASP.NET]更简单的方法:Form ...

  8. jquery简单的图片切换效果,支持pc端、移动端的banner图片切换开发

    详细内容请点击 无意中看见了两年前写的一个图片切换,那会儿刚刚学习网页制作,可以说是我的第一个处女座的jquery图片切换效果.无聊之余对它的宽度稍稍做了一下修改,变成了支持pc端.手机端全屏的ban ...

  9. 【转】asp.net mvc3 简单缓存实现sql依赖

    asp.net mvc3 简单缓存实现sql依赖   议题 随 着网站的发展,大量用户访问流行内容和动态内容,这两个方面的因素会增加平均的载入时间,给Web服务器和数据库服务器造成大量的请求压力.而大 ...

  10. Objective-C ,ios,iphone开发基础:快速实现一个简单的图片查看器

    新建一个single view 工程: 关闭ARC , 在.xib视图文件上拖放一个UIImageView  两个UIButton ,一个UISlider ,布局如图. 并为他们连线, UIImage ...

随机推荐

  1. 数据结构(六)查找---多路查找树(B+树)

    前提 下图B树,我们要遍历它,假设每个节点都属于硬盘的不同页面,我们为了中序遍历所有的元素,页面2-页面1-页面3-页面1-页面4-页面1-页面5.而且我们每经过节点遍历时,都会对节点中的元素进行一次 ...

  2. golang 的时间格式化操作

    怎么做 简而言之 time.Now().Format("2006-01-02 15:04:05") 你将会获得如同 yyyy-MM-dd hh-mm-ss 这样的输出. 还可以 在 ...

  3. Spark记录-Spark-Shell客户端操作读取Hive数据

    1.拷贝hive-site.xml到spark/conf下,拷贝mysql-connector-java-xxx-bin.jar到hive/lib下 2.开启hive元数据服务:hive  --ser ...

  4. POJ No.3255 Roadblocks 求次短路径

    #define _CRT_SECURE_NO_WARNINGS /* 7 10 0 1 5 0 2 2 1 2 4 1 3 2 2 3 6 2 4 10 3 5 1 4 5 3 4 6 5 5 6 9 ...

  5. Nginx动态添加模块

    前言 有时候要使用已安装好的Nginx的功能时,突然发现缺少了对应模块,故需对其进行动态添加模块. 操作 # 查看已安装模块 [root@kazihuo ~]# nginx -V nginx vers ...

  6. Vue加载json文件

    一.在build/dev-server.js文件里 var app = express() 这句代码后面添加如下(旧版): var appData = require('../address.json ...

  7. 汉化DotNetBar中控件的系统文本

    作者:ComponentCN 出处:www.componentcn.com 2011年11月02日 阅读: DotNetBar很多子控件的系统文本.提示信息等都是可以本地化的,可以转化为多种语言,控件 ...

  8. MAC洪水攻击

    MAC洪水攻击原理 传统的交换机在数据转发过程中依靠对CAM表的查询来确定正确的转发接口,一旦在查询过程中无法找到相关的目的MAC对应的条目,此数据帧将作为广播帧来处理,CAM表的容量有限,只能存储不 ...

  9. kafka.common.KafkaException: Socket server failed to bind to hdp1:9092: Cannot assign requested address.

    ERROR [KafkaServer id=1] Fatal error during KafkaServer startup. Prepare to shutdown (kafka.server.K ...

  10. MySQL(二)MySQL的启动或链接失败

    有时候用命令mysql -u root -p 或者服务器启动mysql数据库的时候,会出现抛出异常并失败. 以下是遇过的异常. 1.1)抛出的异常:出现ERROR 2002 (HY000): Can' ...