3

I have images stored in a Sql Server database, and I want to display them on a web page.

The problem is, the Image control in ASP.NET has the property ImageUrl, which takes the value of a path to a folder which contains the image file. But I am drawing my image from a database table, and do not want to save the imge to the disk so I can provide the path. I'd rather stream the image directly to the page.

I'm having a hard time even picturing this (pun intended), and suspect that the only way this is going to get done is for the image to be temp-saved to a location and then pathed to an image control. But maybe I'm missing something?

Edited to Add:

This is for an accounting system where receipts for travel expenses are required to be presented for reimbursement. We are thinking of allowing users (we have 30,000 of them) to scan their receipts and store them in association with their reimbursement requests. Storing this in the file system is, I suppose, one way of doing it, with an index into the file system stored in a Sql Server table. But I've been asked to see if this could be done in Sql Server, and I can do this fine, but the display/report aspect is important. Reports need to be generated for these reimbursement requests, and I don't know how compliant Crystal Reports is with dipping into the file system. In short, it would be much easier to do this in Sql Server.

Cyberherbalist
  • 12,061
  • 17
  • 83
  • 121
  • Are you open to *not* storing them in SQL Server but rather to the file system instead? That could give you better performance in the long run. – Dax Fohl Apr 30 '13 at 19:09
  • possible duplicate of [Streaming VARBINARY data from SQL Server in C#](http://stackoverflow.com/questions/5042589/streaming-varbinary-data-from-sql-server-in-c-sharp) – Alexei Levenkov Apr 30 '13 at 19:13
  • or if you really want to inline image - http://stackoverflow.com/questions/6826390/how-to-convert-image-to-data-uri-for-html-with-c – Alexei Levenkov Apr 30 '13 at 19:16
  • 1
    Either use a data: url or use an url pointing to a .ashx page that takes some parameters and returns the image for you. Note that data: uri is not supported by older versions of IE. – mihi Apr 30 '13 at 19:18
  • Nit: a .ashx file represents a handler, not a page. – John Saunders Apr 30 '13 at 20:52

2 Answers2

2

Have you considered using data URIs? A data URI allows you to embed data like images directly into your page. The image is encoded to base 64 and then included as the source of the <img> element in the HTML. You can read more about data URIs in general here.

The details of how to do this in C# are covered by this question and answer. The image element ends up looking something like <img src="data:image/png;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrS"/>.

The major issue with using data URIs for images is that older version of IE are not supported, or limit the size of the image. Here is documentation of the browser limitations of data URIs.

Community
  • 1
  • 1
Ben Reich
  • 16,222
  • 2
  • 38
  • 59
-1
   <img runat="server" 
             src='<%# "getImage.aspx?ID=" + DataBinder.Eval(Container.DataItem, 
             "ImageIdentity")  %>' ID="Img1"/>

then in getimage.aspx

  sqlConnection.Open();     
  SqlCommand sqlCommand = new SqlCommand("Select Images"  ,sqlConnection);
  Reader= sqlCommand.ExecuteReader(CommandBehavior.CloseConnection); 
  byte [] byteArray
  if(Reader.Read())
  {
     byteArray= (byte[]) Reader["Images"];
  }

  System.IO.MemoryStream mstream = new System.IO.MemoryStream(byteArray,0,byteArray.Length);
  System.Drawing.Image dbImage = System.Drawing.Image.FromStream( new System.IO.MemoryStream(byteArray));
  System.Drawing.Image thumbnailImage = dbImage.GetThumbnailImage(100,100,null,new System.IntPtr());
   thumbnailImage.Save(mstream,dbImage.RawFormat);
   Byte[] thumbnailByteArray = new Byte[mstream.Length];
   mstream.Position = 0;
   mstream.Read(thumbnailByteArray, 0, Convert.ToInt32(mstream.Length));
   Response.Clear();
   Response.ContentType="image/jpeg";
   Response.BinaryWrite(thumbnailByteArray);
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Shafqat Masood
  • 2,532
  • 1
  • 17
  • 23