Wednesday, 18 July 2012

How to convert data in binary to image format in asp.Net using c#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Diagnostics;
using System.Web;
public class HandlerStuImage : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        SqlConnection con = new SqlConnection(@"data source=.\sqlexpress; initial catalog=AdminUse; integrated security=SSPI;");
        con.Open();
        string sql = "select imagepath from binaryimg where id=@id";
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.Parameters.Add("@id", SqlDbType.Int).Value = context.Request.QueryString["Eid"];
        cmd.Prepare();
        SqlDataReader dr = cmd.ExecuteReader();
        dr.Read();
        context.Response.BinaryWrite((byte[])dr["imagepath"]);
        dr.Close();
        con.Close();
    }

    public bool IsReusable
    {
        get { return false; }
    }

No comments:

Post a Comment