1.服务器上传图片
html前端页面代码:
如果要上传,就必须 设置 表单 method=post,而且 enctype=multipart/form-data ,
一旦设置了 enctype=multipart/form-data,那么浏览器生成请求报文的时候,就会生成 数据分割符并且更换 请求报文体 的数据 组织格式(使用 分隔符 来 分开不同 html 表单控件的 内容)
<body>
<form method="post" action="Upload.ashx" enctype="multipart/form-data">
<input type="file" name="file01" />
<input type="file" name="file02" />
<input type="submit" value="上传" />
</form>
</body>
后台代码:Upload.ashx页面
上传大图和缩略图,并且大图上添加水印
public void ProcessRequest(HttpContext context)
{
System.Text.StringBuilder sbMsg = new System.Text.StringBuilder(200);
//浏览器端上传文件的集合:Request.Files
//1.遍历所有上传来的文件
for (int i = 0; i < context.Request.Files.Count; i++)
{
HttpPostedFile file = context.Request.Files[i];
//判断文件大小
if (file.ContentLength > 0)
{
//判断上传的文件 是否 为 图片
//通过 判断文件的 类型
if (file.ContentType.Contains("image/"))
{
//2.为图片加水印
//2.1获取图片文件流,并封装到 C# 的 Image对象中 方便操作
using (Image img = Image.FromStream(file.InputStream))
{
string strImgName = file.FileName;
string strThmImbName = "";
GetRandomName(ref strImgName,ref strThmImbName);
//2.2生成缩略图
using (Image imgThumb = new Bitmap(200,100))
{
//2.2.1生成 画家对象,要它在 缩略图上作画
using (Graphics g = Graphics.FromImage(imgThumb))
{
// 原图 , 要把原图缩略成多大 , 取原图的哪个部分 来缩略 ,单位(像素)
g.DrawImage(img, new Rectangle(0, 0, imgThumb.Width, imgThumb.Height), new RectangleF(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
}
//先获取物理路径
string phyPath = context.Request.MapPath("/upload/" + strThmImbName);
//保存
imgThumb.Save(phyPath);
}
//3.读取 水印 图片,画到 上传的图片上
using (Image imgWater = Image.FromFile(context.Server.MapPath("/img/logo.jpg")))
{
using (Graphics g = Graphics.FromImage(img))
{
//g.DrawString(".Net上传图片", new Font("微软雅黑", 14), Brushes.Red, 0, 0);
//将 水印图片 画到 上传的图上
g.DrawImage(imgWater, 0, 0);
}
//先获取物理路径
string phyPath = context.Request.MapPath("/upload/" + strImgName);
//保存
img.Save(phyPath);
sbMsg.AppendLine(strImgName + "<br/>");
}
}
//file.SaveAs(phyPath);
}
}
}
context.Response.Write("保存成功啦:" + sbMsg.ToString());
}
void GetRandomName(ref string imgName,ref string thumbName)
{
string fileName =Guid.NewGuid().ToString();
string extention = System.IO.Path.GetExtension(imgName);
//生成原图名
imgName = fileName + extention;
//生成缩略图名
thumbName = fileName + "+thm" + extention;
}
2.读取请求文件并提示浏览器另存为:强制浏览器以另存为的方式打开本次输出的响应报文
public void ProcessRequest(HttpContext context)
{
//1.获取要下载的文件路径
string strFilePath = context.Request.QueryString["name"];
//2.转成物理路径
strFilePath = context.Request.MapPath(strFilePath);
//关键:添加一个相应报文头,强制浏览器 以另存为附件 的方式 打开 本次输出的响应报文
context.Response.AddHeader("Content-Disposition", "attachment;filename=gzitcast.jpg");
//3.将文件读取并输出给浏览器
context.Response.WriteFile(strFilePath);
}
3.一般处理程序生成验证码图片
public class ValidCode : IHttpHandler
{
char[] charArr = null;
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/jpeg";
//初始化 字符
charArr = new char[10] { 'a', 'b', 'c', '1', '2', '3', '北', '京', '上', '海' };
//生成随机验证码字符串
string code = MakeRanStr();
//生成验证码图片
using (Image img = new Bitmap(100, 35))
{
using (Graphics g = Graphics.FromImage(img))
{
g.Clear(Color.White);
DrawGanRaoDian(50, g, img);
g.DrawRectangle(Pens.Black, new Rectangle(0, 0, img.Width-1, img.Height-1));
g.DrawString(code, new Font("汉仪菱心体简", 16), Brushes.Red, 0, 2);
DrawGanRaoDian(50, g, img);
}
//将生成 图片 保存到 响应报文体流中,以jpg图片格式保存
img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
// 随机对象
Random ran = new Random();
/// <summary>
/// 随机生成字符串
/// </summary>
/// <returns></returns>
string MakeRanStr()
{
System.Text.StringBuilder sbCode = new System.Text.StringBuilder(5);
int index = -1;
for (int i = 0; i < 5; i++)
{
index = ran.Next(charArr.Length);
sbCode.Append(charArr[index]);
}
return sbCode.ToString();
}
/// <summary>
/// 画干扰点
/// </summary>
/// <param name="count"></param>
/// <param name="g"></param>
/// <param name="img"></param>
void DrawGanRaoDian(int count, Graphics g, Image img)
{
for (int i = 0; i < count; i++)
{
Point p1 = new Point(ran.Next(img.Width - 5), ran.Next(img.Height - 5));
Point p2 = new Point(p1.X - ran.Next(10), p1.Y - ran.Next(10));
g.DrawLine(Pens.Black, p1, p2);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}