views:

32

answers:

1

I get the data of the stored image on database as byte[] array; then I convert it to System.Drawing.Image like the code shown below;

  public System.Drawing.Image CreateImage(byte[] bytes)
        {

            System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(bytes);
            System.Drawing.Image image = System.Drawing.Image.FromStream(memoryStream);
            return image;
        }

(*) On the other hand I am planning to show a list of images on asp.net pages as the client scrolls downs the page. The more user gets down and down on the page he/she does see the more photos. So it means fast page loads and rich user experience. (you may see what I mean on www.mashable.com, just take care the new loads of the photos as you scroll down.)

Moreover, the returned imgae object from the method above, how can i show it in a loop dynamically using the (*) conditions above.

Regards bk

+1  A: 

Well, I think the main bottleneck is actually hitting the database each time you need an image. (Especially considering many users accessing the site.)

I would go with the following solution:

  1. Database will store images with the original quality;
  2. .ashx handler will cache images on the file system in various needed resolutions (like 32x32 pixels for icons, 48x48 for thumbnails, etc.) returning them on request and accessing database only once; (in this example is shown how to return an image via ashx handler)
  3. The actual pages will point to .ashx page to get an image. (like <img scr="GetImage.ashx?ID=324453&Size=48" />)

UPDATE:

So the actual workflow in the handler will be like:

    public void ProcessRequest (HttpContext context)
    {
        // Create path of cached file based on the context passed
        int size = Int32.Parse(context.Request["Size"]);
        // For ID Guids are possibly better
        // but it can be anything, even parameter you need to pass
        // to the web service in order to get those bytes
        int id = Int32.Parse(context.Request["Id"]);
        string imagePath = String.Format(@"images/cache/{0}/{1}.png", size, id);

        // Check whether cache image exists and created less than an hour ago
        // (create it if necessary)
        if (!File.Exists(imagePath)
            || File.GetLastWriteTime(imagePath) < DateTime.Now.AddHours(-1))
        {
            // Get the file from the web service here
            byte[] imageBytes = ...

            // Save as a file
            using (var memoryStream = new MemoryStream(imageBytes))
            using (var outputStream = File.OpenWrite(imagePath))
                Image.FromStream(memoryStream).Save(outputStream);
        }

        context.Response.ContentType = "image/png";
        context.Response.WriteFile(imagePath);
    }
Regent
@Regent; The bytes are provided by a web service, so how can I integrate it to ashx file?regards bk
blgnklc
@blgnklc: I think you still need to cache images on the file system, initially getting them from the web service if not cached. (Possibly adding some expiration, like "ignore cache image if file created yesterday".)
Regent
@Regent thanks for your kind help.
blgnklc