步步为营-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 ...
随机推荐
- switchyomega插件的基本使用
switchyomega插件的基本使用 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 作为一名合格的开发工程师,使用插件本应该就是手到擒来的事情,可能刚刚入开发门槛的人,在使用插件 ...
- JAVA记录-redis缓存机制介绍(三)
Redis 事务 Redis 事务可以一次执行多个命令, 并且带有以下两个重要的保证: 事务是一个单独的隔离操作:事务中的所有命令都会序列化.按顺序地执行.事务在执行的过程中,不会被其他客户端发送来的 ...
- div中让内容能不换行就尽量不换行.【纯原】
div中让内容能不换行就尽量不换行,部分左对齐,部分右对齐. <html> <head> <title>九歌·少司命</title> <style ...
- bzoj千题计划304:bzoj3676: [Apio2014]回文串(回文自动机)
https://www.lydsy.com/JudgeOnline/problem.php?id=3676 回文自动机模板题 4年前的APIO如今竟沦为模板,,,╮(╯▽╰)╭,唉 #include& ...
- 流媒体技术学习笔记之(一)nginx+nginx-rtmp-module+ffmpeg搭建流媒体服务器
参照网址: [1]http://blog.csdn.net/redstarofsleep/article/details/45092147 [2]HLS介绍:http://www.cnblogs.co ...
- postgresql 常用速查
中文资料 中文资料 /**gp中的基本sql语法**/ --删除表 drop table testtb; --创建表 CREATE TABLE testtb ( id integer, "n ...
- Jquery中click事件重复执行的问题
平常没注意事件绑定问题,在此注意一下: function testClick(obj){ $("select").off().on("click", funct ...
- Nginx 防盗链配置
防盗链一般都是流媒体配置 location ~* \.(jpg|jpeg|png|bmg|swf|mp4|mp4|mmf|zip|rar|swf|flv)$ { // 对jpg|jpeg|png|bm ...
- Java 学习札记(二)TomCat安装配置
1.下载TomCat 下载地址:http://tomcat.apache.org/ 2.配置环境变量 CATALINA_HOME:F:\JAVA\apache-tomcat-6.0.18\apache ...
- Thymeleaf在IDEA中的使用
让html页面的thymeleaf 标签不显示刺眼的红色波浪线,解决方法如下 IDEA官方链接:https://www.jetbrains.com/help/idea/2017.1/thymeleaf ...