<1>一般处理程序的图片处理
<pre class="csharp" name="code">using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
//using System.Drawing;
namespace ASP.NET中级
{
/// <summary>
/// Handler2 的摘要说明
/// </summary>
public class Handler2 : IHttpHandler
{
//注意 system.Drawing是可以using进来的,为了理解,只是我这里没有使用此方式而已
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/JPEG";
string fullPath = context.Request.MapPath("~/images/123.jpg"); //因为是自己定义了300*100的画布,所以这个图片没有用到
//-----------------------------------------------------------------------
//创建一个300*100画布.默认背景颜色为黑色
using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap (300,100))
{
//从指定的 Image 创建新的 Graphics【绘图画面】
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//------------------------------如果不需要填充颜色,这一步可以不要--------------------
//将画布的颜色填充为红色(其实也可以用g.Clear(Color.Red)来做)0,0是坐标,300,100是填充画布区域的大小,因为画布的大小也是300*100,所以就将整个画布全部填充满了。
g.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.Red), 0, 0, 300,100 );
//清除整个绘画图面,以红色填充(这样整个画布就变成红色的背景了)
//g.Clear(System.Drawing.Color.Red);
//-------------------------------如果不需要在图片写画一些东西,这一步可以不要--------------
//DrawString意思是:在指定位置并且用指定的 Brush和Font对象绘制指定的文本字符串。Brushes类:所有标准颜色的画笔,属性为颜色
g.DrawString("我制作的图片", new System.Drawing.Font("黑体", 30), System.Drawing.Brushes.Black, 0, 0);
//-----------------------------------------------------------------------
//将图像以指定的格式(System.Drawing.Imaging.ImageFormat.Jpeg)保存到指定的流中(context.Response.OutputStream)
bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
Graphics类包含在System.Drawing名称空间下,Graphics对象表示GDI+绘图表面,是用于创建图形图像的对象。创建要绘制的图形对象,需要先创建 Graphics对象,然后才可以使用GDI+绘制线条和形状、呈现文本或显示与操作图像。
处理图形包括两个步骤:创建Graphics对象和使用Graphics对象绘制线条和形状、呈现文本或显示与操作图像。
1.创建 Graphics 对象
在ASP.NET中可以从任何由Image类派生的对象创建Graphics对象。通过调用System.Drawing.Graphics.FromImage(System.Drawing.Image)方法,提供要从其创建 Graphics对象的Image变量的名称,代码如下:
Bitmap |
获得图形对象引用之后,即可绘制对象、给对象着色并显示对象。
2.使用Graphics对象绘制线条和形状、呈现文本或显示与操作图像
使用Graphics对象绘制线条和形状、呈现文本或显示与操作图像,所用到的属性和方法如表所示。
表
详情:http://blog.csdn.net/jax_lee/article/details/6749746
--------------------------------------------------------------------------------------------------------------------
假如现在有一幅图片(原图),那么我要在这幅图片上加上另外一幅图片(水印图)如何做呢?
客户端
<!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>
<a href="Handler1.ashx">请求一副图片</a><br />
</form>
</body>
</html>
服务端
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
namespace 下载文件
{
/// <summary>
/// Handler1 的摘要说明
/// </summary>
public class Handler1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "Image/Jpeg";
//获取已经有的图片的路径
string fullPath = context.Request.MapPath("images/yuantu.jpg");
//把图片从指定磁盘路径读取到内存中
using (System.Drawing.Image img = System.Drawing.Image.FromFile(fullPath))
{
//读取水印图片
using (Image imgWater = Image.FromFile(context.Server.MapPath("images/shuiyin.jpg")))
{
//创建一个“画布”
using (Graphics g = Graphics.FromImage(img))
{
//把水印图片画到原图上
//第一个参数是:要绘制的 System.Drawing.Image。(即:水印图片)
//第二个参数是:它指定所绘制图像的位置和大小。 将图像进行缩放以适合该矩形。(即:将水印图片放到原图的哪个位置)
//第三个参数是:它指定 image 对象中要绘制的部分。
//第四个参数是:枚举的成员,它指定 srcRect 参数所用的度量单位。(Pixel表示将设备像素指定为度量单位)
g.DrawImage(imgWater, new Rectangle(100, 100, imgWater.Width, imgWater.Height),
new Rectangle(0,0,imgWater.Width,imgWater.Height),GraphicsUnit.Pixel);
}
}
img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
图中的小美女这张图是水印图,鞋子这张图是原图,现在是把水印图叠加到原图上
缩略图的制作
客户端:HtmlPage1.html
<!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="Handler1.ashx" method="post" enctype="multipart/form-data">
<input type="file" name="fileName" value="" />
<input type="submit" value="上传"/>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
namespace 缩略图
{
/// <summary>
/// Handler1 的摘要说明
/// </summary>
public class Handler1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
HttpPostedFile file= context.Request.Files["fileName"];
string fileName = DateTime.Now.ToString("yyyyMMdd") + "_" + file.FileName;
string filePath = context.Server.MapPath("大图");
//保存上传的原始大图
file.SaveAs(filePath + "\\" +fileName );
//根据用户上传的文件创建一个Image对象,这个对象是用户上传的原图的对象
//file.InputStream:获取一个 System.IO.Stream 对象,该对象指向一个上载文件,以准备读取该文件的内容。
using (Image imgBig = Image.FromStream(file.InputStream))
{
//创建一个小图片对象,这个是一个略缩图
using(System.Drawing.Bitmap imgSmall=new Bitmap((imgBig.Width/5),(imgBig.Height/5)))
{
//基于小图片,创建一个“画布”
using (Graphics g = Graphics.FromImage(imgSmall))
{
//第二个参数表示:将imgBit这个大图画到小图片上,0,0表示将大图画到小图片的哪个位置(坐标),imgSmall.Width, imgSmall.Height则表示:将大图画到小图上,是按照小图片的具体大小来画,即小图片有多大,我就画多大。
g.DrawImage(imgBig, new Rectangle(0, 0, imgSmall.Width, imgSmall.Height));
}
//保存原始大图(其实我们也可以在这里保存原始大图,但是上面已经保存大图了。这里就不需要再保存了)
//imgBig.Save(filePath + "\\" + fileName);
string smallPath = context.Server.MapPath("小图");
//保存小图
imgSmall.Save(smallPath+"\\"+fileName);
context.Response.Write("OK");
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
---------------------
图片类型验证
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApp
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//如何判断用户提交过来的数据类型是否是图片类型。所以这里做一个类型校验
HttpPostedFile file= Request.Files["fileName"];
//获取文件的扩展名
string ext = System.IO.Path.GetExtension(file.FileName);
//获取文件名
string fileName=System.IO.Path.GetFileName(file.FileName);
if ((ext==".jpg"|| ext==".bmp" || ext==".png"|| ext == ".gif"||ext==".jpeg" ) &&file.ContentType.StartsWith("image"))
{
//创建大图片
using (System.Drawing.Image imgBig = System.Drawing.Image.FromStream(file.InputStream))
{
int bwidth = imgBig.Width;
int bheight = imgBig.Height;
//创建一个小图片
using (System.Drawing.Image imgSmall = new System.Drawing.Bitmap(100, 100 * bheight / bwidth))
{
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(imgSmall))
{
g.DrawImage(imgBig, 0, 0, imgSmall.Width, imgSmall.Height);
}
//把大图,和小图都保存到磁盘上。
//先给这两个文件起个新的文件名。
string bigFileName=Guid.NewGuid().ToString()+"_大图"+fileName;
//小图片的名字
string smallFileName=Guid.NewGuid().ToString()+"_小图"+fileName;
int hashcode=bigFileName.GetHashCode();
int dir1=hashcode & 0xf;
int dir2=(hashcode>>4) & 0xf;
//设定保存到磁盘上的路径
string bigFilePath=Server.MapPath("Upload/BigPic/"+dir1+"/"+dir2);
string samllFilePath=Server.MapPath("Upload/SamllPic/"+dir1+"/"+dir2);
//保存大图
imgBig.Save(System.IO.Path.Combine(bigFilePath, samllFilePath));
//保存小图
imgSmall.Save(System.IO.Path.Combine(bigFilePath, samllFilePath));
}
}
}
}
}
}