2013-10-29 60 views
0

我要创造我自己的动态横幅动态横幅,于是我开始制作图片处理程序,ATM我有这样的代码:创建图像处理程序

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

namespace Faeria 
{ 
/// <summary> 
/// Summary description for FaeriaImage 
/// </summary> 
public class FaeriaImage : IHttpHandler 
{ 

    public void ProcessRequest(HttpContext context) 
    { 
     context.Response.ContentType = "image/jpeg"; 
     context.Response.Write("~/Images/bg1.jpg"); 
    } 

    public bool IsReusable 
    { 
     get 
     { 
      return false; 
     } 
    } 
} 

}

但是当我打电话“ http:// localhost:12361/FaeriaImage.ashx“我只得到这个:http://abload.de/image.php?img=1deib0.jpg

当我在我的网站中调用它时,我没有任何图像。

这是什么错误?

回答

1

我已经使用过处理程序,据我所知,您需要在处理程序中绘制图像。这段代码可以帮助你,因为它帮助了我。试试吧

using (Bitmap image = new Bitmap(context.Server.MapPath("~/Images/bg1.jpg"))) 
{ 
    using(MemoryStream ms = new MemoryStream()) 
    { 
     image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 
     ms.WriteTo(context.Response.OutputStream); 
    } 
} 
0

更改您的代码如下(不会写,但WriteFile的)

public void ProcessRequest(HttpContext context) 
{ 
    context.Response.ContentType = "image/jpeg"; 
    context.Response.WriteFile("~/Images/bg1.jpg"); 
}