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. shell中的循环语法

    shell中的循环语法              作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.for循环 1.语法格式1 for 变量 in 值1 值2 值3 ... do ...

  2. logstash收集IIS日志

    匹配字段 %{TIMESTAMP_ISO8601:log_timestamp} (%{WORD:s-sitename}|-) (%{IPORHOST:s-ip}|-) (%{WORD:cs-metho ...

  3. VNC 在ubuntu desktop下只显示空白桌面

    看不到上下的菜单栏,但是有桌面.要么是配置文件,要么是gnome缺组件. 1.先安装组件 apt-get install --no-install-recommends ubuntu-desktop ...

  4. python---自定义字段验证

    各个字段类,含正则和验证方法 #字段类 class IPField: REGULAR = "^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4] ...

  5. javascript 事件冒泡与取消冒泡

    事件冒泡: 当一个元素上的事件被触发时,比如说鼠标点击了一个按钮,同样的事件将会在那个元素的所有祖先中被触发,这一过程被称为事件冒泡. 这个事件从原始祖先开始,一直冒泡到DOM树的最上层.(bug) ...

  6. css postion 属性区别【原】

    CSS样式中的postion元素有四个属性,即static | absolute | fixed | relative. static: 默认值.无特殊定位,遵循HTML基本定位规则 . fixed: ...

  7. 让div固定在顶部不随滚动条滚动【转】

    让div固定在顶部不随滚动条滚动 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...

  8. authentication failed for xxx错误

    现象: 公司windows定期修改过密码后 一直报错.push的时候显示“Authentication Failed for http://x.x.x.x/x/git” 猜想: 发现可能是账号问题. ...

  9. 《Two Dozen Short Lessons in Haskell》所有习题的索引

    <Two Dozen Short Lessons in Haskell>(Copyright © 1995, 1996, 1997 by Rex Page,有人翻译为Haskell二十四学 ...

  10. Java之Jacob调用COM接口DLL-----------------------------------dm。dll

    用Java控制windows了,嗯,低层次按键模拟,可控制游戏,内存也不在话下. 一.环境介绍 1.myeclipse8.5 2.著名按键插件dm.dll  32bit.此插件实现COM接口,百度百科 ...