0

Work on C# ASP.NET. I have a problem with repeater control. I want to show images on a page that is stored in a database. I collect information from northwind database.

Categories table

SQL syntax:

CREATE TABLE [dbo].[Categories](
    [CategoryID] [int] IDENTITY(1,1) NOT NULL,
    [CategoryName] [nvarchar](15) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [Description] [ntext] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [Picture] [image] NULL)

On pageload C# syntax :

 NorthWindDataContext db = new NorthWindDataContext();
            List<Category> oList = new List<Category>();
            oList = db.Categories.ToList();
            Repeater1.DataSource = oList;

aspx Syntax:

 <ItemTemplate>
            <tr>
                <td>
                   <%#DataBinder.Eval(Container, "DataItem.Picture")%>
                </td>

            </tr>
        </ItemTemplate>

After run the code,in my page i don't get the picture.help me to get picture on my page .Thanks in advance .If have any query plz ask.

shamim
  • 6,640
  • 20
  • 85
  • 151
  • You're trying to output binary data onto the page. For an image to be displayed in HTML, you need to Since that image is binary data, you'd likely have to set up a handler to server up that image data and then point to the handler in the source. – Doozer Blake Oct 07 '11 at 16:10

1 Answers1

2

You are store images as binary data in a data base so you can not show it as is.

MSDN on image data type:

Variable-length binary data from 0 through 2^31-1 (2,147,483,647) bytes.

The common approach is to create instance of the System.Drawing.Image from a memory stream and then write it explicitly inr response. This functionality should be wrapped by ASHX handler. See intrawebs for the examples, there are a lot.

Handler pseudo code below: (then in ASPX just reffer handler)

IEnuemrable<byte> binaryImageData = queryExecutor.GetImageData(imageId);

// do not forget to dispose or use 'using'
var memoryStream = new MemoryStream(); 
memoryStream.Write(imageByte, 0, binaryImageData.Length);
System.Drawing.Image image = System.Drawing.Image.FromStream(memoryStream);

// ... create JPEG

context.Response.ContentType = "image/jpeg";
jpegImage.Save(context.Response.OutputStream, 
               System.Drawing.Imaging.ImageFormat.Jpeg);

Good examples I've found:

Community
  • 1
  • 1
sll
  • 61,540
  • 22
  • 104
  • 156