开发者

An image from byte to optimized web page presentation

开发者 https://www.devze.com 2022-12-29 05:41 出处:网络
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;

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;
   开发者_StackOverflow     }

(*) 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


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);
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消