图像的通用处理程序(ashx) - 除非先写入图像,否则非常慢

时间:2014-07-21 04:19:17

标签: asp.net webforms ashx

我多年来一直在从sql server渲染二进制图像,但通常只有非常小的图像。最近,我想为更大的图像(500kb大小)这样做。

我发现我正在做的旧方式需要花费很长时间(45秒)来渲染图像。新的方式很快。我在下面列出了两种方法。任何想法为什么第一个这么慢?我应该提一下,我更喜欢渲染图像的原始方式,因为它的内容类型是不可知的。

在这两种情况下,ImageData都存储为sql server 2008中的varbinary。

缓慢的方式

        context.Response.Clear();
        context.Response.ContentType = user.ImageContentType;

        context.Response.Cache.SetCacheability(HttpCacheability.Public);
        context.Response.Cache.SetMaxAge(new TimeSpan(0, 5, 0));

        context.Response.OutputStream.Write((byte[])user.ImageData, 0, user.ImageData.Length);
        context.Response.End();

快速方式

            context.Response.Cache.SetCacheability(HttpCacheability.Public);
            context.Response.Cache.SetMaxAge(new TimeSpan(0, 5, 0));

            MemoryStream memoryStream = new MemoryStream((byte[])prm.ImageData, false);
            System.Drawing.Image imgFromGB = System.Drawing.Image.FromStream(memoryStream);
            imgFromGB.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

            context.Response.Cache.SetCacheability(HttpCacheability.Public);
            context.Response.Cache.SetMaxAge(new TimeSpan(0, 5, 0));

0 个答案:

没有答案