How to pass binary image to image handler to display it in DataList?
-
I have a web page with a method called to display some images in the DataList control
MyImage.cs
class MyImage
{
string Name { get; set; }
byte[] Jpeg { get; set; }
}MyImages.aspx.cs
pubic void DisplayMyImages(IEnumerable myImages)
{
this.myImagesDataList.DataSource = myImages;
this.myImagesDataList.DataBind();
}
...
protected void myImagesDataList_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
MyImage myImage = (MyImage)e.Item.DataItem;
Image myImageImage = (Image)e.Item.FindControl("myImageImage");// How to pass myImage.Jpeg to ImageHandler? form here myImageImage.ImageUrl = "~/Handlers/ImageHandler.ashx"; }
}
But how to pass jpeg image to ImageHandler if it is already extracted from the database and passed to DisplayMyImages() function? Remarks: I do not want to save them back to files and pass the paths in a query string to ImageHandler Having a standard query string approach is not possible as I do not want to violate model view presenter approach
Чесноков
-
I have a web page with a method called to display some images in the DataList control
MyImage.cs
class MyImage
{
string Name { get; set; }
byte[] Jpeg { get; set; }
}MyImages.aspx.cs
pubic void DisplayMyImages(IEnumerable myImages)
{
this.myImagesDataList.DataSource = myImages;
this.myImagesDataList.DataBind();
}
...
protected void myImagesDataList_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
MyImage myImage = (MyImage)e.Item.DataItem;
Image myImageImage = (Image)e.Item.FindControl("myImageImage");// How to pass myImage.Jpeg to ImageHandler? form here myImageImage.ImageUrl = "~/Handlers/ImageHandler.ashx"; }
}
But how to pass jpeg image to ImageHandler if it is already extracted from the database and passed to DisplayMyImages() function? Remarks: I do not want to save them back to files and pass the paths in a query string to ImageHandler Having a standard query string approach is not possible as I do not want to violate model view presenter approach
Чесноков
Depending on how big your images are, and which browsers you need to support, you might be able to use a data URI[^]. (If you're using IE8, you're limited to 32Kb; earlier versions of IE aren't supported at all.)
myImageImage.ImageUrl = "data:image/jpeg;base64," + Convert.ToBase64String(myImage.Jpeg);
If you need to support older versions of IE, you'll need to pass a query-string to your handler. If the image is more than 32Kb, it'll be too large for a query-string, so you'll need to pass some kind of ID to allow the handler to retrieve the image.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Depending on how big your images are, and which browsers you need to support, you might be able to use a data URI[^]. (If you're using IE8, you're limited to 32Kb; earlier versions of IE aren't supported at all.)
myImageImage.ImageUrl = "data:image/jpeg;base64," + Convert.ToBase64String(myImage.Jpeg);
If you need to support older versions of IE, you'll need to pass a query-string to your handler. If the image is more than 32Kb, it'll be too large for a query-string, so you'll need to pass some kind of ID to allow the handler to retrieve the image.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
that is interesting approach! never thought of it before
Чесноков