title: C# Displaying Image from Database
body: <p>I am having some issues displaying an image from a SQL Server database in a .NET application using C#. I've got the save part of the image working and it is storing the image as a series of byes in the database, but now I am running into issues trying to display it. Here is what I have:</p>
  
  <pre><code>using System;
  using System.Configuration;
  using System.Web;
  using System.IO;
  using System.Data;
  using System.Data.SqlClient;
  
  public class ShowImage : IHttpHandler
  {
      public void ProcessRequest(HttpContext context)
      {
          Int32 empno;
          if (context.Request.QueryString["id"] != null)
              empno = Convert.ToInt32(context.Request.QueryString["id"]);
          else
              throw new ArgumentException("No parameter specified");
  
          context.Response.ContentType = "image/jpeg";
          Stream strm = ShowEmpImage(empno);
          byte[] buffer = new byte[4096];
          int byteSeq = strm.Read(buffer, 0, 4096);
  
          while (byteSeq &gt; 0)
          {
              context.Response.OutputStream.Write(buffer, 0, byteSeq);
              byteSeq = strm.Read(buffer, 0, 4096);
          }
          //context.Response.BinaryWrite(buffer);
      }
  
      public Stream ShowEmpImage(int empno)
      {
          string conn = CodProbs.Main.GetDSN();
          SqlConnection connection = new SqlConnection(conn);
          string sql = "SELECT CoverPhoto FROM Galleries WHERE GalleryID = @GalleryID";
          SqlCommand cmd = new SqlCommand(sql, connection);
          cmd.CommandType = CommandType.Text;
          cmd.Parameters.AddWithValue("@GalleryID", empno);
          connection.Open();
          byte[] img =                                                                   System.Text.Encoding.Unicode.GetBytes(Convert.ToString(cmd.ExecuteScalar()));
      try
      {
          return new MemoryStream((byte[])img);
      }
      catch
      {
          return null;
      }
      finally
      {
          connection.Close();
      }
  }
  
  public bool IsReusable
  {
      get
      {
          return false;
      }
  }
  </code></pre>
  
  <p>This is not resulting in any syntax errors and seems like it should be working. After stepping through it with the debugger, I can see that it is grabbing the proper data from the database. However, I receive an error of: "The image ... cannot be displayed because it contains errors."</p>
  
  <p>Any ideas on what the issue is here?</p>
  
  <p><strong>UPDATE</strong> Storing the image</p>
  
  <pre><code>public static int AddGallery(GalleryDS galleryDS)
          {
              DataRow gallery = galleryDS.Tables[0].Rows[0];
              int result = 0;
              string sql = @"insert into Galleries (Title, Description,     GalleryCategoryID, CreateDate, CreatedBy, CoverPhoto)
                          values (@Title, @Description, @GalleryCategoryID, @CreateDate, @CreatedBy, @CoverPhoto)
                          select scope_identity()";
  
          using (SqlConnection conn = new SqlConnection(Main.GetDSN()))
          {
              SqlCommand command = new SqlCommand(sql, conn);
              command.Parameters.AddWithValue("@Title", gallery["Title"]);
              command.Parameters.AddWithValue("@Description", gallery["Description"]);
              command.Parameters.AddWithValue("@GalleryCategoryID", 0);
              command.Parameters.AddWithValue("@CreateDate", DateTime.Now);
              command.Parameters.AddWithValue("@CreatedBy", gallery["CreatedBy"]);
              command.Parameters.Add("@CoverPhoto", SqlDbType.VarBinary, Int32.MaxValue);
              command.Parameters["@CoverPhoto"].Value = gallery["CoverPhoto"];
              conn.Open();
              result = Convert.ToInt32(command.ExecuteScalar());
              conn.Close();
          }
          return result;
      }
  </code></pre>