0

I'm trying to using a Generic Handler to retrieve and display images that are stored in a database.

But its just not working. Ive tried verious of the code below, but I cant seem to get it to work.

Can anyonne spot what I am doing wrong, or have some suggestions?

<%@ WebHandler Language="C#" Class="IconsDb" %>

using System;
using System.Web;
using System.Linq;
using System.Data.Entity;

public class IconsDb : IHttpHandler {

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

        if (context.Request.QueryString["id"] != null)
            iconId = Convert.ToInt32(context.Request.QueryString["id"]);
        else
            throw new ArgumentException("No parameter specified");

        context.Response.ContentType = "image/gif";
        //System.IO.Stream strm = ShowEmpImage(iconId);

        var db = new UdINaturen.UdINaturenContext();

        var GetIcon = (from i in db.subcategoryicons
                       where i.id == iconId
                       select i.picture).FirstOrDefault();
        object img = GetIcon;

        System.IO.MemoryStream memStream= new System.IO.MemoryStream((byte[])Convert.FromBase64String(GetIcon));
        System.Drawing.Bitmap bitImage=new System.Drawing.Bitmap((System.Drawing.Bitmap)System.Drawing.Image.FromStream(memStream));


        byte[] buffer = memStream.ToArray();
        context.Response.ContentType = "image/gif";
        //context.Response.OutputStream.Write(buffer, 0, buffer.Length);
        //context.Response.WriteFile();
        context.Response.BinaryWrite(buffer);
        //context.Response.Flush();


    }





    public bool IsReusable {
        get {
            return true;
        }
    }

}
2
  • why are you setting your content type to plain text on line 2? Commented Feb 9, 2010 at 18:01
  • why do you convert the byte array to a MemoryStream to a Bitmap to a byte array? Commented Feb 9, 2010 at 18:03

1 Answer 1

3

wow, ok. Not sure how much of that is old code, what should be commented out or w/e but try something like this:

public void ProcessRequest (HttpContext context) {
    int iconId;

    if (string.IsNullOrEmpty(context.Request.QueryString["id"]) || !int.TryParse(context.Request.QueryString["id"], out iconId) )
        throw new ArgumentException("No parameter specified");

    context.Response.ContentType = "image/gif";

    var db = new UdINaturen.UdINaturenContext();

    var GetIcon = (from i in db.subcategoryicons
                   where i.id == iconId
                   select i.picture).FirstOrDefault();
    byte[] buffer = (byte[])Convert.FromBase64String(GetIcon);

    context.Response.ContentType = "image/gif";
    context.Response.BinaryWrite(buffer);
    context.Response.Flush();
}
Sign up to request clarification or add additional context in comments.

2 Comments

You accidentally erased Convert.FromBase64String
I might also have a generic "invalid parameter" image for the handler to return rather than throwing an exception, but that's a personal preference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.