ASP.NET - decode binary value to image - Asked By msakt on 26-Jun-12 03:07 AM

Jitendra Faye replied to msakt on 26-Jun-12 03:08 AM
Use this code to convert byte data to image.

SqlConnection cn = new SqlConnection("Data Source=;Initial Catalog=JitendraDB;User ID=sa;password=change_123");
        string strQuery = "Select * from EmpImage";
        SqlDataAdapter da = new SqlDataAdapter(strQuery, cn);
        DataSet ds = new DataSet();

        byte[] b = new byte[0];
        b = (Byte[])(ds.Tables[0].Rows[0][1

        MemoryStream ms = new MemoryStream(b);

       pictureBox1.Image = Image.FromStream(ms);
msakt replied to Jitendra Faye on 26-Jun-12 03:16 AM
this code put in aspx page or handler page...

pictureBox1.Image = Image.FromStream(ms); this means

its is not here
Jitendra Faye replied to msakt on 26-Jun-12 03:24 AM
The code which I have sent you that is for windows forms,

if you want to  use web form then use this code-

For Retrieving image from database and displaying, you can use Handler.

use following code-

step1 :

protected void Page_Load(object sender, EventArgs e)


Image1.ImageUrl = "handler1.ashx?EmpID='1'";


step2:   handler code

public void ProcessRequest(HttpContext context)


SqlConnection cn = new SqlConnection("YOUR CONNECTION STRING");


string empid = context.Request.QueryString["EmpID"].ToString();

SqlCommand cmd = new SqlCommand("select empimage from empimageTABLE where empid=" + empid, cn);

SqlDataReader dr = cmd.ExecuteReader();






try this code and let me know.

msakt replied to Jitendra Faye on 26-Jun-12 03:28 AM
hi vicky,after complete step1 dont go to handler page...i am using vs2010
so handler page is only one dont have .cs file

like this

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

using System;
using System.Configuration;
using System.Web;
using System.Web.Services;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

[WebService(Namespace = "")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Handler2 : IHttpHandler {

    string strcon = ConfigurationManager.AppSettings["ConnectionString"].ToString();
    public void ProcessRequest (HttpContext context) {

      //string imageid = context.Request.QueryString["ImID"];
      string imageid = "9";

      Database db = DatabaseFactory.CreateDatabase("ConnectionString");

      //DbCommand cmd = db.GetSqlStringCommand("select Imagevalue from tblImage where ID=" + imageid);
      DbCommand cmd = db.GetSqlStringCommand("select BillImage from BillUpload where BillID =" + imageid);
      IDataReader dr = db.ExecuteReader(cmd);
      //context.Response.ContentType = "text/plain";
      //context.Response.Write("Hello World");

Lalit M replied to msakt on 26-Jun-12 04:58 AM
Use this code
private void byteArrayToImage(byte[] byteArrayIn)
      System.Drawing.Image newImage;
      string strFileName = GetTempFolderName() + "yourfilename.gif";
      if (byteArrayIn != null)
        using (MemoryStream stream = new MemoryStream(byteArrayIn))
         newImage = System.Drawing.Image.FromStream(stream);
          img.Attributes.Add("src", strFileName);

        lblMessage.Text = "The image conversion was successful.";
        Response.Write("No image data found!");
Read More:-
Jitendra Faye replied to msakt on 26-Jun-12 05:16 AM
which name you have given for handler , if you are using this code-

Image1.ImageUrl = "handler1.ashx?EmpID='1'";

then handler name should be handler1
dipa ahuja replied to msakt on 26-Jun-12 08:07 AM
In you have to take handler to display binary image :

Step 1: Add a handle in your website and write this code in ProcessRequest:
public void ProcessRequest (HttpContext context)
  SqlConnection con = new SqlConnection();
  con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
  // Create SQL Command
  SqlCommand cmd = new SqlCommand();
  cmd.CommandText = "Select pname,images from Photos where id =@id";
  cmd.CommandType = System.Data.CommandType.Text;
  cmd.Connection = con;
  SqlParameter ImageID = new SqlParameter("@id", System.Data.SqlDbType.Int);
  ImageID.Value = context.Request.QueryString["id"];
  SqlDataReader dReader = cmd.ExecuteReader();
Step 2: Add a Image Control in ItemTemplate:
  <asp:Image ID="Image1" runat="server"
ImageUrl='<%# "Handler.ashx?id=" + Eval("id")%>'/>
//display image from the database using handler
Image2.ImageUrl = "Handler.ashx?id=2";