0

I want to create my own dynamic banner, so I started to create an Image Handler, atm I have this code:

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;
        }
    }
}

}

But when I call "http:// localhost :12361/FaeriaImage.ashx" I only get this: http://abload.de/image.php?img=1deib0.jpg.

And when I call it in my site with I get no image.

Whats is my mistake here?

Nunser
  • 4,512
  • 8
  • 25
  • 37
PassionateDeveloper
  • 14,558
  • 34
  • 107
  • 176

2 Answers2

1

I've used the handler and as far as I know you need to draw the image in the handler. This code might help you as it helped me. Try it out

 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);
   }
 }
polin
  • 2,745
  • 2
  • 15
  • 20
0

Change you code to the following (not write but WriteFile)

public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "image/jpeg";
    context.Response.WriteFile("~/Images/bg1.jpg");
}
Menelaos Vergis
  • 3,715
  • 5
  • 30
  • 46