一般处理程序ashx结合gdi+处理图片
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);
}
}
}