另一个进程正在使用ASP.net c#文件
我使用以下页面(thumboil.ashx)创建缩略图:另一个进程正在使用ASP.net c#文件,c#,asp.net,permission-denied,C#,Asp.net,Permission Denied,我使用以下页面(thumboil.ashx)创建缩略图: <%@ WebHandler Language="C#" Class="Thumbnail" %> using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO; using System.Web; public class Thumbnail : IHttp
<%@ WebHandler Language="C#" Class="Thumbnail" %>
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
public class Thumbnail : IHttpHandler {
private int _thumbnailSize = 150;
public void ProcessRequest(HttpContext context) {
string photoName = context.Request.QueryString["p"];
string cachePath = Path.Combine(HttpRuntime.CodegenDir, photoName + ".png");
if (File.Exists(cachePath)) {
OutputCacheResponse(context, File.GetLastWriteTime(cachePath));
context.Response.WriteFile(cachePath);
return;
}
string photoPath = context.Server.MapPath("../uploads/originals/" + photoName);
Bitmap photo;
try {
photo = new Bitmap(photoPath);
}
catch (ArgumentException) {
throw new HttpException(404, "Photo not found.");
}
context.Response.ContentType = "image/png";
int width, height;
if (photo.Width > photo.Height) {
width = _thumbnailSize;
height = photo.Height * _thumbnailSize / photo.Width;
}
else {
width = photo.Width * _thumbnailSize / photo.Height;
height = _thumbnailSize;
}
Bitmap target = new Bitmap(width, height);
using (Graphics graphics = Graphics.FromImage(target)) {
graphics.CompositingQuality = CompositingQuality.HighSpeed;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.DrawImage(photo, 0, 0, width, height);
using (MemoryStream memoryStream = new MemoryStream()) {
target.Save(memoryStream, ImageFormat.Png);
OutputCacheResponse(context, File.GetLastWriteTime(photoPath));
using (FileStream diskCacheStream = new FileStream(cachePath, FileMode.CreateNew)) {
memoryStream.WriteTo(diskCacheStream);
}
memoryStream.WriteTo(context.Response.OutputStream);
}
}
}
private static void OutputCacheResponse(HttpContext context, DateTime lastModified) {
HttpCachePolicy cachePolicy = context.Response.Cache;
cachePolicy.SetCacheability(HttpCacheability.Public);
cachePolicy.VaryByParams["p"] = true;
cachePolicy.SetOmitVaryStar(true);
cachePolicy.SetExpires(DateTime.Now + TimeSpan.FromDays(365));
cachePolicy.SetValidUntilExpires(true);
cachePolicy.SetLastModified(lastModified);
}
public bool IsReusable {
get {
return true;
}
}
}
使用制度;
使用系统图;
使用System.Drawing.Drawing2D;
使用系统、绘图、成像;
使用System.IO;
使用System.Web;
公共类缩略图:IHttpHandler{
私有int_thumbnailSize=150;
公共void ProcessRequest(HttpContext上下文){
字符串photoName=context.Request.QueryString[“p”];
字符串cachePath=Path.Combine(HttpRuntime.CodegenDir,photoName+“.png”);
if(File.Exists(cachePath)){
OutputCacheResponse(上下文,File.GetLastWriteTime(cachePath));
context.Response.WriteFile(缓存路径);
返回;
}
字符串photoPath=context.Server.MapPath(“../uploads/originals/”+photoName);
位图照片;
试一试{
照片=新位图(光路);
}
捕获(异常){
抛出新的HttpException(404,“找不到照片”);
}
context.Response.ContentType=“image/png”;
int宽度、高度;
如果(照片宽度>照片高度){
宽度=_指钉大小;
高度=照片高度*_缩略图尺寸/照片宽度;
}
否则{
宽度=照片。宽度*_缩略图尺寸/照片。高度;
高度=_指钉尺寸;
}
位图目标=新位图(宽度、高度);
使用(Graphics=Graphics.FromImage(目标)){
graphics.CompositingQuality=CompositingQuality.HighSpeed;
graphics.InterpolationMode=InterpolationMode.HighQualityBicubic;
graphics.CompositingMode=CompositingMode.SourceCopy;
绘图图像(照片,0,0,宽度,高度);
使用(MemoryStream MemoryStream=new MemoryStream()){
Save(memoryStream,ImageFormat.Png);
OutputCacheResponse(上下文,File.GetLastWriteTime(photoPath));
使用(FileStream diskCacheStream=newfilestream(cachePath,FileMode.CreateNew)){
memoryStream.WriteTo(diskCacheStream);
}
memoryStream.WriteTo(context.Response.OutputStream);
}
}
}
私有静态void OutputCacheSponse(HttpContext上下文,DateTime lastModified){
HttpCachePolicy cachePolicy=context.Response.Cache;
cachePolicy.SetCacheability(HttpCacheability.Public);
cachePolicy.VaryByParams[“p”]=true;
cachePolicy.SetOmitVaryStar(true);
SetExpires(DateTime.Now+TimeSpan.FromDays(365));
cachePolicy.SetValidUntilExpires(true);
cachePolicy.SetLastModified(lastModified);
}
公共布尔可重用{
得到{
返回true;
}
}
}
当我试图以物理方式或通过代码删除图片文件时,会出现“无法访问文件,正在被其他进程使用”错误
这是由于缓存缩略图造成的吗?还是文件没有关闭在我无法发现的地方
可能是我的上传文件脚本导致了这种情况吗?您似乎没有在任何地方关闭MemoryStream?垃圾收集本身不会移除文件锁,是吗?在ProcessRequest结束时抛出MemoryStream.Close()不会造成伤害