您的位置:首页 > 其它

一般处理程序ashx结合gdi+处理图片

2014-05-23 15:07 218 查看
1 新建一般处理程序 .ashx

public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");

}

2. 引入命名空间 using System.Drawing;

3.因为是操作图片所以把context.Response.ContentType = "text/plain";改为context.Response.ContentType = "image/jpeg";

4

//一般处理程序中给图片添加文字

string path = context.Request.MapPath("psb.jpg");

using (Image img = Image.FromFile(path))
{
using (Graphics g = Graphics.FromImage(img))
{
g.DrawString("文字", new Font("黑体", 40), Brushes.Red, 120, 120);
img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}

5

//给图片添加logo

string imagepath = context.Request.MapPath("psb.jpg");
string loginpath = context.Request.MapPath("logo.png");

using (Image img=Image.FromFile(imagepath))
{
using (Image logoimage=Image.FromFile(loginpath))
{
using (Graphics g=Graphics.FromImage(img))
{
g.DrawImage(logoimage, 100, 200, logoimage.Width, logoimage.Height);
img.Save(context.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: