Skip to content

Commit

Permalink
修正一个图片加载的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
aiqinxuancai committed Mar 5, 2024
1 parent bb5248b commit d180c14
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions Aria2Fast/Utils/ImageCacheUtils.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System;
using Flurl.Http;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.Caching;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Imaging;

namespace Aria2Fast.Utils
Expand Down Expand Up @@ -72,27 +74,27 @@ public static BitmapImage GetImageWithLocalCache(Uri uri)

BitmapImage img = new BitmapImage();
img.BeginInit();

if (File.Exists(localFilePath))
{
img.UriSource = new Uri(localFilePath);
}
else
{
img.UriSource = uri;
img.DownloadCompleted += (s, e) =>
var result = uri.GetBytesAsync().Result;
if (result != null && result.Length > 0)
{
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create((BitmapImage)s));
using (var fileStream = new FileStream(localFilePath, FileMode.Create))
{
encoder.Save(fileStream);
}
};
img.StreamSource = new MemoryStream(result);
File.WriteAllBytes(localFilePath, result);
}
}

img.CacheOption = BitmapCacheOption.OnLoad;
img.EndInit();
img.Freeze();
if (img.CanFreeze && !img.IsFrozen) // 检查能否冻结,并且还没有被冻结
{
img.Freeze();
}

return img;
}

Expand Down

0 comments on commit d180c14

Please sign in to comment.