public class ImageHandler : IHttpHandler

Posted by Ken on Stack Overflow See other posts from Stack Overflow or by Ken
Published on 2011-01-08T07:33:08Z Indexed on 2011/01/08 7:53 UTC
Read the original article Hit count: 223

Filed under:
|
|

cmd.Parameters.AddWithValue("@id", new system.Guid (imageid));

What using System reference would this require?

Here is the handler:

using System;
using System.Collections.Specialized;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using System.Globalization;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Web.Profile;
using System.Drawing;

public class ImageHandler : IHttpHandler {

public void ProcessRequest(HttpContext context)
{
    string imageid;
    if (context.Request.QueryString["id"] != null)
        imageid = (context.Request.QueryString["id"]);
    else
        throw new ArgumentException("No parameter specified");

    context.Response.ContentType = "image/jpeg";
    Stream strm = ShowProfileImage(imageid.ToString());
    byte[] buffer = new byte[8192];
    int byteSeq = strm.Read(buffer, 0, 8192);

    while (byteSeq > 0)
    {
        context.Response.OutputStream.Write(buffer, 0, byteSeq);
        byteSeq = strm.Read(buffer, 0, 8192);
    }
    //context.Response.BinaryWrite(buffer);
}

public Stream ShowProfileImage(String imageid)
{
    string conn = ConfigurationManager.ConnectionStrings["MyConnectionString1"].ConnectionString;
    SqlConnection connection = new SqlConnection(conn);
    string sql = "SELECT image FROM Profile WHERE UserId = @id";
    SqlCommand cmd = new SqlCommand(sql, connection);
    cmd.CommandType = CommandType.Text;
    cmd.Parameters.AddWithValue("@id", new system.Guid (imageid));//Failing Here!!!!
    connection.Open();
    object img = cmd.ExecuteScalar();
    try
    {
        return new MemoryStream((byte[])img);
    }
    catch
    {
        return null;
    }
    finally
    {
        connection.Close();
    }
}

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

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about httphandler