Exemple #1
0
 private ImageSource LoadCore(string fileName, int width, int height)
 {
     try
     {
         var decode = false;
         if (width != 0 && height != 0 && this.HighQualityResizer)
         {
             fileName = ImageResizer.Resize(fileName, width, height);
         }
         else
         {
             decode = true;
         }
         var source = new BitmapImage();
         source.BeginInit();
         source.CacheOption = BitmapCacheOption.OnLoad;
         //TODO: This is much faster than using UriSource.
         //TODO: See: https://referencesource.microsoft.com/#PresentationCore/Core/CSharp/System/Windows/Media/Imaging/BitmapDecoder.cs,cd1c67c55a73f984
         using (source.StreamSource = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
         {
             if (decode)
             {
                 if (width != 0)
                 {
                     source.DecodePixelWidth = width;
                 }
                 else if (height != 0)
                 {
                     source.DecodePixelHeight = height;
                 }
             }
             source.EndInit();
             source.Freeze();
         }
         return(source);
     }
     catch (Exception e)
     {
         Logger.Write(typeof(ImageLoader), LogLevel.Warn, "Failed to load image: {0}", e.Message);
         return(null);
     }
 }
Exemple #2
0
 private ImageSource LoadCore(string id, string fileName, int width, int height)
 {
     try
     {
         var decode = false;
         if (width != 0 && height != 0 && this.HighQualityResizer)
         {
             fileName = ImageResizer.Resize(id, fileName, width, height);
         }
         else
         {
             decode = true;
         }
         var source = new BitmapImage();
         source.BeginInit();
         source.CacheOption = BitmapCacheOption.OnLoad;
         source.UriSource   = new Uri(fileName);
         if (decode)
         {
             if (width != 0)
             {
                 source.DecodePixelWidth = width;
             }
             else if (height != 0)
             {
                 source.DecodePixelHeight = height;
             }
         }
         source.EndInit();
         source.Freeze();
         return(source);
     }
     catch (Exception e)
     {
         Logger.Write(typeof(ImageLoader), LogLevel.Warn, "Failed to load image: {0}", e.Message);
         return(null);
     }
 }