HttpHandler to return the binary image.
CategoryImages.ashx?CategoryID=1
You can use this call in a src... attribute of the HTML img control or any an ASP.NET Server control that can display images.
Code behind ;
Create a HttpHandler Class called CategoryImages
Create the NorthwinDataContext LINQ to SQL class and add the Category Table to the DataContext layout.
Create the NorthwinDataContext LINQ to SQL class and add the Category Table to the DataContext layout.
/************************************************************/
public class CategoryImages: IHttpHandler
{
// Implement the ProcessRequest method
public void ProcessRequest(HttpContext context)
{
HttpRequest req = context.Request;
string categoryID = req.QueryString["CategoryID"].ToString();
// Get information about the specified category
NorthwinDataContext db = new NorthwinDataContext();
var category = from c in db.Categories
where Convert.ToInt32(c.CategoryID) == Convert.ToInt32(categoryID)
select c.Picture;
int len = category.First().Length;
// Output the binary data
// But first we need to strip out the OLE header
int OleHeaderLength = 78;
int strippedImageLength = len - OleHeaderLength;
byte[] imagdata = new byte[strippedImageLength];
Array.Copy(category.First().ToArray(), OleHeaderLength, imagdata, 0, strippedImageLength);
if ((imagdata) != null)
{
MemoryStream m = new MemoryStream(imagdata);
System.Drawing.Image image = System.Drawing.Image.FromStream(m);
image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
}
}
/*************************************************************/

3 comments:
Thank you,
your post is very helpful.
Tuyen Nguyen
Wow!!! Honestly speaking you are really a great writer. What I required I got it. Thank you so much.MICROSOFT CERTIFICATION Training in Noida
In the year 2024 this is still helping people, Thanks!
Post a Comment