<%@ WebHandler Language="C#" class="HttpHandler访问" %>
using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
public class HttpHandler访问 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/JPEG";
string path = HttpContext.Current.Server.MapPath("1.jpg");//运行到这里出现异常
using (Bitmap bitmap = new Bitmap(path))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.DrawString("IP:" + context.Request.UserHostAddress, new Font("黑体", 30), Brushes.Red, 0, 0
);
}
bitmap.Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
}
}
求解
string path = HttpContext.Current.Server.MapPath("1.jpg");
这个运行异常?不应该的啊?你写成这样看:
string path = HttpContext.Current.Server.MapPath("~/1.jpg");
你那个写法默认对应为:
string path = HttpContext.Current.Server.MapPath("./1.jpg");
都不会有错的。
还是不行啊.我直接往new Bitmap("c:/1.jpg")就没问题。 查过path中路径没问题,但放到MapPath中就出异常。。
终于解决了。还是路径问题。
Bitmap bitmap = new Bitmap("images/"+path)