Show Image on repeater control
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,
[D开发者_Python百科escription] [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.
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:
- SO Post: Dynamically Rendering asp:Image from BLOB entry in ASP.NET
- C# Save and Load Image from Database
精彩评论