Pregunta

puedo crear imágenes en miniatura con la página siguiente, 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;
        }
    }
}

Cuando intento eliminar el archivo de imagen físicamente oa través de código, aparece un archivo no se puede' acceso, siendo utilizado por otro proceso" de error.

Es esto causado por el almacenamiento en caché de la miniatura? ¿O es que el archivo no estar en un lugar cerrado que no puedo detectar?

¿Podría ser mi script de carga de archivos está causando esto?

¿Fue útil?

Solución

No parece ser el cierre de su MemoryStream en cualquier lugar? La recolección de basura por sí sola no va a quitar un bloqueo de archivo, ¿verdad? El sacudir en un memoryStream.Close () al final de ProcessRequest no podría lastimar.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top