When Page_Load is executed I assign the ImageUrl which is a IHTTPHandler
:
imgView.ImageUrl = "ImageHandler.ashx" + "?rezeptId=" + rezeptId.ToString() +"&imageId=0";
Which looks like this:
public class ImageHandler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
int rezeptId = Int32.Parse(context.Request.QueryString["rezeptId"]);
int imageId = Int32.Parse(context.Request.QueryString["imageId"]);
List<Image> images = DBManager.GetRezeptImages(rezeptId);
context.Response.ContentType = "image/png";
context.Response.BinaryWrite(Helper.ImageToByteArray(images[imageId]));
}
public bool IsReusable {
get {
return false;
}
}
}
The handler call is a static method from DBManager
, a custom static class which provides LINQ-To-SQL queries. The method is called GetRezeptImages()
:
public static List<Image> GetRezeptImages(int rezeptId)
{
using (CookBookDataContext ctx = new CookBookDataContext(Resources.ResourceFile.DBConnection))
{
IEnumerable<RezeptBilder> bilder = from b in ctx.RezeptBilders where b.FKRezept == rezeptId select b;
List<Image> imageList = new List<Image>();
foreach (RezeptBilder b in bilder)
{
imageList.Add(Helper.ByteArrayToImage(b.Bild.ToArray()));
}
return imageList;
}
}
This method simply loads image binaries from the database and converts them to Images:
public static Image ByteArrayToImage(byte[] byteArrayIn)
{
using (MemoryStream ms = new MemoryStream(byteArrayIn))
{
Image returnImage = Image.FromStream(ms);
return returnImage;
}
}
So basically this is all that happens when loading images. It is possible that GetRezeptImages()
will return a List with more than one image but it is always only one that is loaded to image control.
But now I am experiencing some delays when loading an image:
Now i wonder what the reason is, here are some things I think could be the reason:
- Images have to be compressed
- My method implementation requires re-engineering.
- Images are simply to big, which is somehow the same as they need to be compressed.
- I am using a trial that my Webhoster provides. I haven't figured out if there is some speed limitations or somkething yet.
So I was hoping that someone with more experience in webdevelopement could give me some advice on this issue.