开发者

load large image from database and return it to the client side

Hi: IN my application,I have some images saved in the db,so I create a ImgDownLoad.aspx to retrive the image and retun them,since the image in the db may very large(some of them is more than 20M),so I generate some thumbnails ,this is the code:

page_load(){
  string id=Requset.QueryString["id"];
  string imgtype=Requset.Querystring["itype"];

  if(imgType=="small")
  {
   //request the thumbnail
    string small_loaction=getSmallLocationById(id);

    if(!File.exists(small_location)
    {
      byte[] img_stream =getStream开发者_JAVA百科FromDb(id);
      Image img=Image.frameStream(new MemsorStream(img_steam));//here,I often get the out of memory error,but I am not sure when it will happen.
      generateSmallImage(img,location)
    }
   Response.TransferFile(small_location);
  }

  else if(imgType=="large"){
    byte[] img_stream =getStreamFromDb(id);
    new MemorySteam(img_stream).writeTo(Response.outputstream);
  } 
}

Anything wrong?

ALso,since I do not know the image format,so I can not add the

Response.contenttype="image/xxx";

What confusing me most is that I will meet the out of memory error,so I change the code:

try{
          byte[] img_stream =getStreamFromDb(id);
          Image img=Image.frameStream(new MemsorStream(img_steam));//here,I often get the out of memory error,but I am not sure when it will happen.
          generateSmallImage(img,location)
}
catche(exceptin e){
 //the small image can not generated,just return the whole image
  new MemorySteam(img_stream).writeTo(Response.outputstream);
  return;
}

In this case,I will avoid the out of memory problem,but some large image can not downloaded sometime.

So I wonder if there are any ways to handle the large image stream?

Take a large image for exmaple:

resolution:12590x4000
size:26M.

In fact,I have opened a large image(almost 24M) with the mspaint,and then save the image again,I found that it size is much smaller than at first. So is it possible to resize the image in the server side? Or other good manners to hanle my problem?


Firstly, you're not disposing of the Image and Stream type instances that you create - given subsequent calls, over time, this is bound to cause issues; particularly with images around the 20meg mark!

Also, why create the thumbnails every call? Create once and cache, or flush to disk: either way, serve a 'one you made earlier' rather than do this processing over and over.

I would recommend, however, you try an minimise the size (in bytes) of the images. Some might argue that they shouldn't be in the database if over 1meg, but store them on disk and a file name in the database. I guess that's open for debate, browse if interested.

To your comment, I'd urge you not to allow other scopes take control of resources 'owned' by another; dispose of items in the scope that creates them (obviously sometimes some things need to stick around, but what is responsible for them should be clear). Here's a little rework of some of your code:

if (imgType == "small")
{
    string small_loaction = getSmallLocationById(id);
    if(!File.exists(small_location)
    {
        byte[] imageBytes = getStreamFromDb(id);
        using (var imageStream = new MemoryStream(imageBytes))
        {
            using (var image = Image.FromStream(imageStream)) 
            {
                generateSmallImage(image, small_location)
            }
        }         
    }
    Response.TransferFile(small_location);
}
else if (imgType=="large")
{
    byte[] imageBytes = getStreamFromDb(id);
    Response.OutputStream.Write(imageBytes, 0, imageBytes.Length);
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜