2012-05-22 117 views
0

删除文件,而我在做下面,我不能更新在Visual Studio 2005无法编辑/目录

这里的文件(noimg100.gif)是代码,

String fileNotFoundPath = context.Server.MapPath("~/common/images/noimg100.gif"); 
context.Response.ContentType = "image/gif"; 
Image notFoundImage = Image.FromFile(fileNotFoundPath); 
notFoundImage.Save(context.Response.OutputStream, ImageFormat.Gif); 

我是否做错了什么或者是否需要在最后处理图像?

编辑: 我发现下面的链接,它说,有关不使用Image.FromFile我用的方式:当您打开从文件的图像 http://support.microsoft.com/kb/309482

+0

您是否收到错误消息? – Default

+0

@默认:共享违规 – Hoque

+0

更新?你正在写映像来响应下载而不更新服务器端,对吗? –

回答

1

,该文件保持打开只要图像存在。由于您不处理对象,它将继续存在,直到垃圾收集器完成并处理它。

Image对象置于代码末尾,可以再次写入文件。

可以使用using块处置的对象,那么你确信它总是会布置,即使在代码中出现错误:

String fileNotFoundPath = context.Server.MapPath("~/common/images/noimg100.gif"); 
context.Response.ContentType = "image/gif"; 
using (Image notFoundImage = Image.FromFile(fileNotFoundPath)) { 
    notFoundImage.Save(context.Response.OutputStream, ImageFormat.Gif); 
} 

而且,你不改变图像以任何方式解压,然后重新压缩它是一种浪费。只需打开文件并将其写入流中:

String fileNotFoundPath = context.Server.MapPath("~/common/images/noimg100.gif"); 
context.Response.ContentType = "image/gif"; 
using (FileStream notFoundImage = File.OpenRead(fileNotFoundPath)) { 
    notFoundImage.CopyTo(context.Response.OutputStream); 
} 
+0

非常感谢。 – Hoque