0

I am trying to retrieve an image from SQL Server 2008 Express and insert it into an <ASP:Image>. I have tried using a MemoryStream but I just can't seem to get it right.

My C# code at the minute looks like:

  try
  {
     con.Open();

     SqlCommand sqlGetStep1 = new SqlCommand("staff_getStep1", con);
     {
         sqlGetStep1.CommandType = CommandType.StoredProcedure;
         sqlGetStep1.Parameters.Add(new SqlParameter("@taskID", Convert.ToInt16(taskID)));
         SqlDataReader step1 = sqlGetStep1.ExecuteReader();

         //Check if username exists
         if (step1.Read())
         {
            step1Text = (string)step1["step1Text"];
            step1Image = (byte)step1["step1Image"];

          }//if 
          else
          {
             step1Text = "null";
             step1Image = 0;
          }//else

           }//sqlDeleteNotification

       }//try
       catch (SqlException sqlEx)
       {
           lblSQLError.Visible = true;
           lblSQLError.Text = sqlEx.Message.ToString();
        }//catach sql
        catch (Exception ex)
       {
           lblError.Visible = true;
           lblError.Text = ex.ToString();

       }//Catch exception
       finally
       {
           con.Close();
       }//Finally

My aspx code where I would like to display the image looks like:

<asp:Panel runat="server" ID="pnlStep1" Visible="false" CssClass="NormalText">        
   <asp:Label runat="server" ID="lblStep1Text" Text="Step 1 Instruction: "></asp:Label>
   <asp:TextBox runat="server" ID="txtStep1Text" Text="" ReadOnly="true"></asp:TextBox>
   <asp:Label runat="server" ID="lblStep1Image" Text="Step 1 Image: "></asp:Label>
   <asp:Image runat="server" ID="imgStep1" ImageUrl="" Height="100px" Width="100px"/>
</asp:Panel>

Any help would be greatly appreciated :)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ciara Murphy
  • 43
  • 2
  • 6
  • possible duplicate of [ASP.NET store Image in SQL and retrieve for Asp:Image](http://stackoverflow.com/questions/698912/asp-net-store-image-in-sql-and-retrieve-for-aspimage) – Sam Axe Jan 28 '13 at 03:47
  • Don't you mean `byte[]`, not `byte`? – lc. Jan 28 '13 at 03:48
  • Code now looks like:The variable - byte[] step1Image = { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }; - get the image value (byte[]) from SQL. do the conversion: MemoryStream ms = new MemoryStream(step1Image); // Stream fs = FileUpload1.PostedFile.InputStream; BinaryReader br = new BinaryReader(ms); Byte[] bytes = br.ReadBytes((Int32)ms.Length); string base64String = Convert.ToBase64String(bytes, 0, bytes.Length); imgStep1.ImageUrl = "data:image/png;base64," + base64String; – Ciara Murphy Jan 29 '13 at 18:52

1 Answers1

2

You need to have a separate url that takes the id of the image to be delivered (in this case the step id) and delivers the contents as the results with the correct MIME type. Use the url to this method as the url for the asp:Image. Normally, you'd implement this (in WebForms) as a HttpHandler. See How to return an image as the response in ASP.NET

Community
  • 1
  • 1
tvanfosson
  • 524,688
  • 99
  • 697
  • 795