Die Speicherung von Bild-im cache

Habe ich ein Bild-handler, aber es hält werfen Fehler

Ich weiß nicht, ob ich das einfügen des Bildes falsch in den cache oder retrieveing es der falsche Weg. Ich bekomme den Puffer null, wenn ich versuche, verwenden Sie die stream.

irgendwelche Ideen?

public void ProcessRequest(HttpContext context)
    {          
        _imageController = new ImageController();

        //Use ImageUrl to request image from external site
        string imageUrl = CastAide.AsString(context.Request["imageUrl"], String.Empty);
        //Use imageGuid and pageid to get image from user response upload
        string imageGuid = CastAide.AsString(context.Request["image"], String.Empty);
        int pageId = CastAide.AsInt32(context.Request["pageId"], -1);
        string subFolder = CastAide.AsString(context.Request["sub"], String.Empty);
        //Use ImageVaultId to return an imageVault Image
        int imageVaultId = CastAide.AsInt32(context.Request["imageVaultId"], 0);
        //Width and height determine the image size rendered
        int width = CastAide.AsInt32(context.Request["width"], 200);
        int height = CastAide.AsInt32(context.Request["height"], 200);
        bool resizeNoCrop = CastAide.AsBoolean(context.Request["resizeonly"], false);

        string cacheKey = "";
        Bitmap image = null;

    //Generate cache key
    if (!String.IsNullOrEmpty(imageGuid))            
        cacheKey = String.Format("{0}_{1}", imageGuid, "guid");                           
    else if (!String.IsNullOrEmpty(imageUrl))            
        cacheKey = String.Format("{0}_{1}", imageUrl, "url");                              
    else if (imageVaultId > 0)            
        cacheKey = String.Format("{0}_{1}", imageVaultId, "vault");                 

    //Load from cache
    if (context.Cache[cacheKey] != null)
    {           
        //Load from cache
        //MemoryStream ms = new MemoryStream((byte[])context.Cache[cacheKey]);
        //image = (Bitmap)Bitmap.FromStream(ms);
        image = context.Cache[cacheKey] as Bitmap;                             
    }
    else
    {
        if (!String.IsNullOrEmpty(imageGuid))
        {
            //load file from the local file store
            FileInfo fi;

            if (!String.IsNullOrEmpty(subFolder))
                fi = new FileInfo(_imageController.GetFilePath(subFolder, imageGuid));
            else
                fi = new FileInfo(_imageController.GetFilePath(pageId, imageGuid));

            if (fi.Exists)
                image = (Bitmap) Bitmap.FromFile(fi.FullName);
            else
                image = (Bitmap) Bitmap.FromFile(ConfigurationManager.AppSettings["ImageNotFoundPath"]);
        }
        else if (!String.IsNullOrEmpty(imageUrl))
        {
            //load file from the internet
            try
            {
                HttpWebRequest request = (HttpWebRequest) WebRequest.Create(imageUrl);
                WebResponse response = request.GetResponse();
                Stream stream = response.GetResponseStream();
                image = (Bitmap) Bitmap.FromStream(stream);
                response.Close();
            }
            catch
            {
                image = (Bitmap) Bitmap.FromFile(ConfigurationManager.AppSettings["ImageNotFoundPath"]);
            }
        }
        else if (imageVaultId > 0)
        {                   
            string filePath = ImageVaultUtility.GetSourceFileName(imageVaultId);
            FileInfo fi = new FileInfo(filePath);

            if (fi.Exists)                    
                image = (Bitmap) Bitmap.FromFile(fi.FullName);                    
            else                    
                image = (Bitmap) Bitmap.FromFile(ConfigurationManager.AppSettings["ImageNotFoundPath"]);                    
        }

        //Insert image into cache
        context.Cache.Insert(cacheKey, image, null, DateTime.Now.AddSeconds(10), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);    
        }


        if (resizeNoCrop)
            ImageUtility.ApplyResizeTransform(ref image, width, height, true);
        else
            ImageUtility.ApplyCropAndResizeTransform(ref image, width, height);

        context.Response.Clear();
        context.Response.ContentType = "image/jpeg";
        image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
        image.Dispose();

    }

beim abrufen der cache ist Folgendes passiert:

Die Speicherung von Bild-im cache

  • Können Sie uns sagen, genau das, was Fehler wird geworfen und bei welcher Codezeile?
  • Versuchen Sie, um das Bild innerhalb von 10 Sekunden, dass es verfügbar ist? context.Cache.Insert(cacheKey, image, null, DateTime.Now.AddSeconds(10), ... bedeutet, das Bild wird aus dem cache entfernt werden 10 Sekunden nach dem es eingefügt wird. Wie @comptent_tech erwähnt, wir brauchen bessere info bezüglich der Ausnahmen, um Ihnen zu helfen viel weiter...
  • gemacht haben, Bearbeiten Sie zeigen Probleme. Dank
InformationsquelleAutor okenshield | 2011-12-09
Schreibe einen Kommentar