我创建缩略图用下面的页面,Thumbnail.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 : 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;
        }
    }
}

当我尝试删除该图像文件物理地或通过代码,得到了一个“不能访问文件,正由另一个过程”的错误。

时这引起由缩略图的缓存?或者是不是封闭的地方我不能当场文件?

难道是我上传的文件脚本导致此?

有帮助吗?

解决方案

您似乎没有被关闭您的MemoryStream的地方?垃圾收集本身不会删除文件锁,不是吗?在memoryStream.Close()在的ProcessRequest的端辗转反侧不能伤害。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top