예제 #1
0
 bool TryGetThumb(WebData webData, MDBImage img, string fileName)
 {
     try
     {
         //try to load thumb
         var    hash          = Base32.Safe.Encode(Hash.FromString(Hash.Type.SHA256, fileName));
         string thumbFileName = FileSystem.Combine(mdb.CacheFolder, "Thumbs", hash + ".jpg");
         var    mime          = MimeTypes.FromExtension(".jpg");
         if (File.Exists(thumbFileName))
         {
             try
             {
                 webData.Answer = WebAnswer.Raw(webData.Request, WebMessage.Create(webData.Method, thumbFileName), File.ReadAllBytes(thumbFileName), mime);
                 webData.Answer.SetCacheTime(TimeSpan.FromDays(1));
                 return(true);
             }
             catch { /*file access error, writing in progress ?, wait for lock and retry*/ }
         }
         //wait until last thumb generation is finished
         byte[] data;
         lock (ThumbCreateSyncRoot)
         {
             //do a second check after lock is released...
             if (File.Exists(thumbFileName))
             {
                 try
                 {
                     webData.Answer = WebAnswer.Raw(webData.Request, WebMessage.Create(webData.Method, thumbFileName), File.ReadAllBytes(thumbFileName), mime);
                     webData.Answer.SetCacheTime(TimeSpan.FromDays(1));
                     return(true);
                 }
                 catch { /*file access error, recreate thumb*/ }
             }
             //generate thumb
             using (var bmp = Bitmap32.FromFile(fileName))
             {
                 data = WebImage.RenderThumb(bmp, thumbFileName);
             }
         }
         webData.Answer = WebAnswer.Raw(webData.Request, WebMessage.Create(webData.Method, thumbFileName), data, mime);
         webData.Answer.AllowCompression = false;
         webData.Answer.SetCacheTime(TimeSpan.FromDays(1));
         return(true);
     }
     catch (Exception ex)
     {
         this.LogError(ex, "Could not load / create thumb for {0}", fileName);
     }
     return(false);
 }
예제 #2
0
        IList <Item> LoadImages(string path)
        {
            var results = new List <Item>();

            foreach (string file in Directory.GetFiles(path, "*.png"))
            {
                Trace.TraceInformation("Load {0}", file);
                var bitmap = Bitmap32.FromFile(file);
                results.Add(new Item()
                {
                    FileName = file, Bitmap = bitmap
                });
            }
            return(results);
        }
예제 #3
0
 public void LoadImage(string fileName)
 {
     if (fileName != imageFileName)
     {
         try
         {
             using (Bitmap32 image = Bitmap32.FromFile(fileName))
             {
                 Sprite.LoadTexture(image);
             }
             imageFileName = fileName;
         }
         catch (Exception ex)
         {
             Trace.TraceError("Error on loading image: " + ex.Message);
         }
     }
 }