步步为营-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 ...
随机推荐
- java中equals和compareTo的区别---解惑
大多转载自 百度知道,个人整理以便日后阅读. value1.compareTo(value2) == 0 value1.equals(value2) equals的效率高些,compareTo其实就是 ...
- JS获取客户端IP地址、MAC和主机名【转】
JS获取客户端IP地址.MAC和主机名 引用:JS获取客户端IP地址.MAC和主机名的7个方法汇总 利用搜狐接口 <html> <head> <meta http-equ ...
- Elasticsearch之中文分词器插件es-ik的自定义词库
它在哪里呢? 非常重要! [hadoop@HadoopMaster custom]$ pwd/home/hadoop/app/elasticsearch-2.4.3/plugins/ik/config ...
- 1.springboot:入门程序
一.Spring Boot 简介 官网英文: Spring Boot makes it easy to create stand-alone, production-grade Spring base ...
- Github安全整理(转载)
刚好这两天对之前github上关注的一些比较有意思的项目进行了一下分类整理,在这里列出来分享给大家,希望能对大家寻找工具或者资源有所帮助. 大部分Repo是关于安全以及Python的,也有一些其他主题 ...
- Rancher
Rancher Docker容器管理平台:图像化管理平台. centos server 10.100.10.10 docker node 10.100.10.15 安装 docker search ...
- 主窗口QMainWindow和启动画面
在较为大型复杂,功能较多的应用程序中,我们通常继承QMainWindow类来进行开发.该主窗口为搭建应用用户界面提供了非常好的框架,请看下图: 可以看出该主窗口类为我们提供了菜单栏(Menu Bar) ...
- flash GC
所有应用程序都要管理内存.应用程序的内存管理包括用于确定何时分配内存,分配多少内存,何时将内容放入回收站,以及何时清空回收站的准则.MMgc是 Flash Player用于几乎所有内存分配工作的通用内 ...
- mysql 案例 ~ 分析执行完的大事务
一 简介:今天咱们来聊聊如何定位以及执行完的大事务 二 目的:通过分析binlog脚本来定位执行的大事务 三 分析脚本 mysqlbinlog --base64-output=decode-rows ...
- python标准库 - 数学库和随机数库
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 我们已经在Python运算中看到Python最基本的数学运算功能.此外,math包 ...