步步为营-71-asp.net的简单练习(图片处理)
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的简单练习(图片处理)的更多相关文章
- ASP.NET 实现简单的图片防盗链介绍
在此,网站图片防盗链的方法是,通过获取Http请求头中的 Referer 标头与本网站域名比较,来判断用户是否来自本站跳转过来的 . 创建一个全局处理程序,用来处理images目录下的图片的直接请求: ...
- ASP.NET Core 简单实现七牛图片上传(FormData 和 Base64)
ASP.NET Core 简单实现七牛图片上传(FormData 和 Base64) 七牛图片上传 SDK(.NET 版本):https://developer.qiniu.com/kodo/sdk/ ...
- 用Asp.net实现简单的文字水印
用Asp.net实现简单的文字水印 经常看见MOP上有人贴那种动态的图片,就是把一个字符串作为参数传给一个动态网页,就会生成一个带有这个字符串的图片,这个叫做文字水印.像什么原来的熊猫系列,还有后来 ...
- 一般处理程序生成简单的图片验证码并通过html验证用户输入的验证码是否正确
一般处理程序生成简单的图片验证码并通过html验证用户输入的验证码是否正确 最近没事研究了下验证码的的动态生成及通过cookie实现HTML页面对用户输入的验证码的校验,简要如下: 1.写 ...
- Asp.Net Core Web Api图片上传(一)集成MongoDB存储实例教程
Asp.Net Core Web Api图片上传及MongoDB存储实例教程(一) 图片或者文件上传相信大家在开发中应该都会用到吧,有的时候还要对图片生成缩略图.那么如何在Asp.Net Core W ...
- Expression Blend4经验分享:制作一个简单的图片按钮样式
这次分享如何做一个简单的图片按钮经验 在我的个人Silverlight网页上,有个Iphone手机的效果,其中用到大量的图片按钮 http://raimon.6.gwidc.com/Iphone/de ...
- [ASP.NET]更简单的方法:FormsAuthentication登录ReturnUrl使用绝对路径
转自:http://www.cnblogs.com/dudu/p/formsauthentication-returnurl-absoluteuri.html [ASP.NET]更简单的方法:Form ...
- jquery简单的图片切换效果,支持pc端、移动端的banner图片切换开发
详细内容请点击 无意中看见了两年前写的一个图片切换,那会儿刚刚学习网页制作,可以说是我的第一个处女座的jquery图片切换效果.无聊之余对它的宽度稍稍做了一下修改,变成了支持pc端.手机端全屏的ban ...
- 【转】asp.net mvc3 简单缓存实现sql依赖
asp.net mvc3 简单缓存实现sql依赖 议题 随 着网站的发展,大量用户访问流行内容和动态内容,这两个方面的因素会增加平均的载入时间,给Web服务器和数据库服务器造成大量的请求压力.而大 ...
- Objective-C ,ios,iphone开发基础:快速实现一个简单的图片查看器
新建一个single view 工程: 关闭ARC , 在.xib视图文件上拖放一个UIImageView 两个UIButton ,一个UISlider ,布局如图. 并为他们连线, UIImage ...
随机推荐
- 8、Python-函数
定义 def printInfo(): print("人生苦短,我用Python") 调用 def printInfo(): print("人生苦短,我用Python&q ...
- win2003服务器安全设置教程
服务器安全设置 1.系统盘和站点放置盘必须设置为NTFS格式,方便设置权限. 2.系统盘和站点放置盘除administrators 和system的用户权限全部去除. 3.启用windows自带防火墙 ...
- Nginx 内核优化
内核参数的优化示例: /etc/sysctl.conf net.ipv4.tcp_max_tw_buckets = // timewait的数量,默认是180000. net.ipv4.ip_loca ...
- 近几年杭电OJ大型比赛题目合集【更新到2017年11月初】
2017年: 区域赛网络赛 6194~6205 6206~6216 区域赛网络赛 6217~6229 2016年: 区域赛网络赛 5868~5877 5878~5891 5 ...
- TCP传输协议
TCP是主机对主机层的传输控制协议,提供可靠的连接服务,采用三次握手确认建立一个连接,四次挥手断开连接. 三次握手 是指建立一个TCP连接时,需要客户端和服务端总共发送3个包以确认连接建立成功.在so ...
- 数据库运维平台~inception回滚功能
一 简介:inception的另一个激动人心的功能,很强大.二 功能简介: inception会针对已经执行sql语句进行1 记录 2 生成回滚语句三 备份: 1 启用远程备份机制(强烈建议一台单 ...
- Database学习 - mysql 数据库 索引
索引 索引在mysql 中也叫 '键',是存储引擎用来快速找到记录的一种数据结构.索引对于良好的性能非常关键,尤其是当表中的数据量越来越大时,索引对于性能的影响愈发重要. 索引优化应该是对查询性能优化 ...
- Apple Watch 开发详解
Apple Watch 开发详解 Apple Watch现在对于第三方开发者来说更多的还是一块额外的屏幕.暂时WatchKit没有能给出足够的接口.现在Watch App的主要运算逻辑需要依赖iPho ...
- DSO windowed optimization 代码 (4)
5 "step"计算 参考<DSO windowed optimization 公式>,计算各个优化变量的增加量. 公式再写一下: \[\begin{align} \b ...
- ubuntu14.04 下安装 gsl 科学计算库
GSL(GNU Scientific Library)作为三大科学计算库之一,除了涵盖基本的线性代数,微分方程,积分,随机数,组合数,方程求根,多项式求根,排序等,还有模拟退火,快速傅里叶变换,小波, ...