Suppose you have a C# class for storing images, such as![]()
public class MyClass
{
public int ID { get; set; }
public byte[] MyImage { get; set; }
}
which is a wrapper class for a table storing images as binary blobs (MS SQL image or varbinary(max), Oracle BLOB etc.)
I found 4 different ways (that is, “simple ways”; certainly there are countless other more complex alternatives) to display the binary data as images, using DevExpress’ ASPxImage and ASPxBinaryImage controls inside a ASPxGridView:
<dx:ASPxGridView ID="gridImages" runat="server"
AutoGenerateColumns="False" KeyFieldName="ID">
<Columns>
<dx:GridViewDataTextColumn Caption="ID" FieldName="ID">
</dx:GridViewDataTextColumn>
using a query like
var images = database.Query<MyImages>().OrderBy(r => r.ID); gridImages.DataSource = images; gridImages.DataBind();
Using GridViewDataBinaryImageColumn
<dx:GridViewDataBinaryImageColumn FieldName="MyImage">
</dx:GridViewDataBinaryImageColumn>
Using ASPxBinaryImage inside a DataItemTemplate
<dx:GridViewDataColumn FieldName="MyImage" >
<DataItemTemplate>
<dx:ASPxBinaryImage ID="img" runat="server"
Value='<%# Eval("MyImage") %>'>
</dx:ASPxBinaryImage>
</DataItemTemplate>
</dx:GridViewDataColumn>
ASPxBinaryImage uses the Value property to store a byte array, which the DevExpress framework translates into <img src> where src requests a DevExpress-implemented URL to serve the byte stream.
If you prefer ASP:Image or ASPxImage to ASPxBinaryImage, you need to write an .ashx handler to retrieve the image data:
public class MyImageHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
int id = 0;
int.TryParse(context.Request["id"], out id);
var image = database.FirstOrDefault<MyClass>(c => c.ID == id);
if (image == null)
{
context.Response.Clear();
return;
}
context.Response.ContentType = "image/png";
context.Response.BinaryWrite(image.MyImage);
context.Response.End();
}
This .ashx handler is invoked by setting the ImageUrlFormatString of a GridViewDataImageColumn
<dx:GridViewDataImageColumn FieldName="ID">
<PropertiesImage ImageUrlFormatString="myimage.ashx?id={0}">
</PropertiesImage>
</dx:GridViewDataImageColumn>
or by setting the ImageUrl property of an ASPxImage:
<dx:GridViewDataColumn >
<DataItemTemplate>
<dx:ASPxImage runat="server" ID="imgTemplate"
ImageUrl='<%# "myimage.ashx?id=" + Eval("ID") %>'>
</dx:ASPxImage>
</DataItemTemplate>
</dx:GridViewDataColumn>
</Columns>
Posted by devio 


