using System;
using System.Web;
using System.Drawing;
返 回的是img标签
context.Response.ContentType = "text/html";
string html = "<img src=\" 图片路径 \" />";
context.Response.Write(html);
返回真正的图片本身
context.Response.ContentType = "image/png";
context.Response.WriteFile("图片路径");
完全动态生成一张图片
context.Response.ContentType = "image/png";
string name = "张三";
//获取客户端ip
string ip = context.Request.UserHostAddress;
//动态生成图片,Image类是抽象类,不能实例化,这里是实例化他的子类,300是高度,100是宽度
using (Image image=new Bitmap(300,100))
{
//无论要向该图片上画任何内容,都需要获取图片的‘画布’
Graphics g=Graphics.FromImage(image);
//设置背景色
g.Clear(Color.Blue);
//设置高质量的图片
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//向图片写姓名、ip,10,10代表坐标,red代表字体颜色红色,NewLine换行(建议使用,不建议\r\n)
g.DrawString(name + Environment.NewLine + ip, new Font("全新硬笔行书简", 20), Brushes.Red, 10, 10);
//把创建好的图片发给用户,保存到输出流,设置图片格式是jpeg
image.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
给图片加一个图片水印
context.Response.ContentType = "text/plain";
//1.获取用户上传的图片
HttpPostedFile file = context.Request.Files[0];
//根据用户上传文件的字节,创建一个图片
using (Image originalImg=Image.FromStream(file.InputStream))
{
//向图片写入水印(可以是文字或者图片)
Graphics g = Graphics.FromImage(originalImg);
//根据一个磁盘文件创建一个图片对象
using(Image syImg=Image.FromFile(context.Request.MapPath("相对路径")))
{
//画图片
g.DrawImage(syImg,0,0);
}
//加一个图片水印
originalImg.Save(context.Request.MapPath("文件夹名") + Guid.NewGuid() + System.IO.Path.GetFileName(file.FileName));
}
}
动态创建验证码
前端代码
<head runat="server">
<title></title>
<script type="text/javascript">
function GetImage() {
document.getElementById('img1').src = "Handler3.ashx?id=" + Math.random();
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<img id="img1" src="Handler3.ashx" alt="Alternate Text" />
<a href="#" onclick="GetImage()">看不清?</a>
</div>
</form>
</body>
</html>
后台代码
using System;
using System.Web;
using System.Drawing;
using System.IO;
public void ProcessRequest (HttpContext context) {
Random rdn = new Random();
CreatYAM(rdn.Next(1000, 10000).ToString(), context);
}
/// <summary>
/// 创建验证码
/// </summary>
/// <param name="validateCode"></param>
/// <param name="context"></param>
public void CreatYAM(string validateCode, HttpContext context)
{
Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * 12.0), 22);
Graphics g = Graphics.FromImage(image);
try
{
//随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.Wheat);
//画图片的干扰线
for (int i = 0; i < 25; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
Font font = new Font("Arial", 10, (FontStyle.Bold | FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height),
Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(validateCode, font, brush, 3, 2);
//画图片的前景干扰点
for (int i = 0; i < 100; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
//保存图片数据
MemoryStream stream = new MemoryStream();
image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
//输出图片流
context.Response.Clear();
context.Response.ContentType = "image/jpeg";
context.Response.BinaryWrite(stream.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
缩略图
前端代码
<form id="form1" runat="server" action="Handler.ashx" method="post" enctype="multipart/form-data">
<div>
<input type="file" name="fileData" value="" />
<input type="submit" value="上传" />
</div>
</form>
后台代码
using System;
using System.Web;
using System.Drawing;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
//获取上传的文件流
HttpPostedFile file = context.Request.Files[0];
//根据文件流创建一个图片(原始图片)
using (Image origimalImage=Image.FromStream(file.InputStream))
{
//获取原始图片的宽和高
int oWidth = origimalImage.Width;
int oHeight = origimalImage.Height;
//缩略图的宽
int tWidth = 100;
int tHeight = oHeight * 100 / oWidth;
//根据原始图片,等比例创建小图片
using (Image thumbnailImage=new Bitmap(tWidth,tHeight))
{
//把大图片的内容画到小图片上
//基于小图创建一个画布对象
Graphics gThumbnail = Graphics.FromImage(thumbnailImage);
//把大图画到小图上
gThumbnail.DrawImage(origimalImage, 0, 0, tWidth, tHeight);
//保存2张图片
origimalImage.Save(context.Request.MapPath("../Upload/") + Guid.NewGuid() + "_origianl_" + System.IO.Path.GetFileName(file.FileName));
thumbnailImage.Save(context.Request.MapPath("../Upload/") + Guid.NewGuid() + "_thumbnail_" + System.IO.Path.GetFileName(file.FileName))
}
}
context.Response.Write("文件上传成功!");
}
public bool IsReusable {
get {
return false;
}
}
}
本文来自博客园,作者:NE_STOP,转载请注明原文链接:https://www.cnblogs.com/alineverstop/p/18004710