Expertize Essentials Innovation through knowledge

15Jul/094

Showing Image from Database in ASP.NET

Add the following lines in ASP.NET file

<asp:image id="imgUserPhoto" imageurl="image.ashx?id=xvd21w54" runat="server"/>
or
<img src="http://www.blogger.com/image.ashx?id=xvd21w54" />

Create an image.ashx file and add the following code in it.

<%@ WebHandler Language="C#" %>
using System;
using System.Configuration;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;

public class image : IHttpHandler
{
  public void ProcessRequest(HttpContext context)
  {
    string id;
    if (context.Request.QueryString["id"] != null)
    {
      id = Convert.ToString(context.Request.QueryString["pid"]);
    }
    else
      throw new ArgumentException("No parameter specified");

    context.Response.ContentType = "image/jpeg";
    Stream Photo = GetPhoto(id, photonumber);
    if (Photo != null)
    {
      byte[] buffer = new byte[4096];
      int byteSeq = Photo.Read(buffer, 0, 4096);

      while (byteSeq > 0)
      {
        context.Response.OutputStream.Write(buffer, 0, byteSeq);
        byteSeq = Photo.Read(buffer, 0, 4096);
      }
    }
    else
    {
      //Loading default Image
      Byte[] imgByte = File.ReadAllBytes(context.Server.MapPath("upload_photo.JPG"));

      Photo = new MemoryStream(imgByte);
      byte[] buffer = new byte[4096];
      int byteSeq = Photo.Read(buffer, 0, 4096);

      while (byteSeq > 0)
      {
        context.Response.OutputStream.Write(buffer, 0, byteSeq);
        byteSeq = Photo.Read(buffer, 0, 4096);
      }

    }
    //context.Response.BinaryWrite(buffer);
  }
  public Stream GetPhoto(string Id, string PhotoNumber)
  {
    string conn = "server=yourservername; database=yourdatabasename; uid=userid; pwd=yourpassword;";
    SqlConnection connection = new SqlConnection(conn);

    string sqlQuery = "SELECT Photo FROM userphoto WHERE id=@id";

    SqlCommand cmd = new SqlCommand(sqlQuery, connection);
    cmd.CommandType = CommandType.Text;
    cmd.Parameters.AddWithValue("@id", Id);
    connection.Open();
    try
    {
      object img = cmd.ExecuteScalar();
      return new MemoryStream((byte[])img);
    }
    catch
    {
      return null;
    }
    finally
    {
      connection.Close();
    }
  }
  public bool IsReusable
  {
    get
    {
      return false;
    }
  }
}
Comments (4) Trackbacks (0)
  1. Mozilla Firefox 3.5 Windows XP

    Very good article. I stuck on the same problem. However now solved by looking this article.

    Well, how can we save an image to the database?

    Regards,
    Kaushik

  2. Internet Explorer 6.0 Windows XP

    Just want to say what a great blog you got here!
    I’ve been around for quite a lot of time, but finally decided to show my appreciation of your work!

    Thumbs up, and keep it going!

    Cheers
    Christian, iwspo.net

  3. Internet Explorer 6.0 Windows XP

    Just want to say what a great blog you got here!
    I’ve been around for quite a lot of time, but finally decided to show my appreciation of your work!

    Thumbs up, and keep it going!

    Cheers
    Christian,Diet Guide!

  4. Internet Explorer 6.0 Windows XP

    Just want to say what a great blog you got here!
    I’ve been around for quite a lot of time, but finally decided to show my appreciation of your work!

    Thumbs up, and keep it going!

    Cheers
    Christian,Earn Free Vouchers / Cash


Leave a comment

(required)

No trackbacks yet.