Asp.net 从数据库加载大图像并将其返回到客户端
你好: 在我的应用程序中,我在数据库中保存了一些图像,因此我创建了一个ImgDownLoad.aspx来检索图像并重新运行它们,因为数据库中的图像可能非常大,其中一些图像超过了20M,因此我生成了一些缩略图,代码如下:Asp.net 从数据库加载大图像并将其返回到客户端,asp.net,image,download,Asp.net,Image,Download,你好: 在我的应用程序中,我在数据库中保存了一些图像,因此我创建了一个ImgDownLoad.aspx来检索图像并重新运行它们,因为数据库中的图像可能非常大,其中一些图像超过了20M,因此我生成了一些缩略图,代码如下: page_load(){ string id=Requset.QueryString["id"]; string imgtype=Requset.Querystring["itype"]; if(imgType=="small") { //request
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 =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)
}
Response.TransferFile(small_location);
}
else if(imgType=="large"){
byte[] img_stream =getStreamFromDb(id);
new MemorySteam(img_stream).writeTo(Response.outputstream);
}
}
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;
}
有什么问题吗
另外,由于我不知道图像格式,所以我无法添加
Response.contenttype="image/xxx";
最让我困惑的是,我会遇到内存不足错误,因此我更改了代码:
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 =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)
}
Response.TransferFile(small_location);
}
else if(imgType=="large"){
byte[] img_stream =getStreamFromDb(id);
new MemorySteam(img_stream).writeTo(Response.outputstream);
}
}
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;
}
在这种情况下,我将避免内存不足的问题,但一些大型图像有时无法下载
因此,我想知道是否有任何方法来处理大型图像流
为exmaple拍摄一张大图:
resolution:12590x4000
size:26M.
事实上,我已经用mspaint打开了一个大约24米的大图像,然后再次保存该图像,我发现它的大小比最初小得多。那么,是否可以在服务器端调整图像大小?或者以其他好的方式来解决我的问题?首先,您不属于您创建的映像和流类型的实例-鉴于后续调用,随着时间的推移,这必然会导致问题;特别是20毫克左右的图像
还有,为什么每次通话都要创建缩略图?创建一次并刷新到磁盘:无论是哪种方式,都要提供一个“您以前制作的一个”,而不是一次又一次地进行此处理
但是,我建议您尝试最小化图像的大小(以字节为单位)。有些人可能会争辩说,如果超过1meg,它们不应该在数据库中,而是将它们存储在磁盘上,并在数据库中存储一个文件名。我想这是可以讨论的
对于您的评论,我强烈建议您不要让其他作用域控制另一个“拥有”的资源;在创建项目的范围内处理项目显然有时候有些事情需要保留,但应该明确哪些是导致这些项目的原因。下面是对一些代码的一点修改:
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);
}
谢谢你的关注。事实上,我在generateSmallImage方法中处理它们。我做了缓存,我将缩略图保存在一个文件夹中,当我请求图像下载请求时,我将检查该缓存文件是否存在,如果存在,将其返回,如果不生成小图像,如果生成小图像失败,我将重新运行整个图像流到客户端。另外,我刚刚拥有数据库的读取权限,数据库的所有者是其他人。感谢您的编码,事实上我是c语言的新手,我以前使用过java,所以我从未使用过using var这个词。。