Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 图像的通用处理程序(ashx)-非常慢,除非先写入图像_Asp.net_Webforms_Ashx - Fatal编程技术网

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

Asp.net 图像的通用处理程序(ashx)-非常慢,除非先写入图像,asp.net,webforms,ashx,Asp.net,Webforms,Ashx,多年来,我一直在从SQLServer渲染二进制图像,但通常只渲染非常小的图像。最近,我想对更大的图像(500kb大小)执行此操作 我发现我以前的方法需要很长的时间(45秒)来渲染图像。新方法很快。我在下面介绍了这两种方法。你知道为什么第一个这么慢吗?我应该提到,我更喜欢原始的图像呈现方式,因为它与内容类型无关 在这两种情况下,ImageData在sql server 2008中都存储为varbinary 慢行方式 context.Response.Clear();

多年来,我一直在从SQLServer渲染二进制图像,但通常只渲染非常小的图像。最近,我想对更大的图像(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));
快速方式

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

顺便说一下,确切的图像大小是:356081,内容类型是:image/jpeg