WPF WeakReference

Install-Package MessagePack
Install-Package Newtonsoft.json

 

public class ImgUrlToImgSourceConverter : IValueConverter
{
    Dictionary<string, WeakReference<BitmapImage>> imgCache = new Dictionary<string, WeakReference<BitmapImage>>();
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        try
        {
            string? imgUrl = value?.ToString();
            if (!string.IsNullOrEmpty(imgUrl) && File.Exists(imgUrl))
            {
                if (imgCache.TryGetValue(imgUrl, out var weakRef) && weakRef.TryGetTarget(out var cachedImg))
                {
                    return cachedImg;
                }
                BitmapImage bmi = new BitmapImage();
                bmi.BeginInit();
                bmi.UriSource = new Uri(imgUrl, UriKind.RelativeOrAbsolute);
                bmi.CacheOption = BitmapCacheOption.OnDemand;
                bmi.CreateOptions = BitmapCreateOptions.DelayCreation;
                bmi.EndInit();
                if (bmi.CanFreeze)
                {
                    bmi.Freeze();
                }
                imgCache[imgUrl] = new WeakReference<BitmapImage>(bmi);
                return bmi;
            }
            return null;
        }
        catch (Exception ex)
        {
            return null;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

 

image

 

 

 

 

 

 

image

 

 

 

 

 

 

image

 

 

 

 

 

 

 

 

image

 

 

2026-03-29T22:13:36.6861304+08:00,start:0,end:49999999,batch size:1000000,batch_count:51
'WpfApp9.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\10.0.4\System.Numerics.Vectors.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WpfApp9.exe' (CoreCLR: clrhost): Loaded 'D:\C\WpfApp9\WpfApp9\bin\Debug\net10.0-windows\Newtonsoft.Json.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WpfApp9.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\10.0.4\System.Runtime.Numerics.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WpfApp9.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\10.0.4\System.Linq.Expressions.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WpfApp9.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\10.0.4\System.Data.Common.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
2026-03-29 22:13:37,startIdx:0,endIdx:1000000,batch:1
2026-03-29 22:13:38,startIdx:1000000,endIdx:2000000,batch:2
2026-03-29 22:13:39,startIdx:2000000,endIdx:3000000,batch:3
2026-03-29 22:13:40,startIdx:3000000,endIdx:4000000,batch:4
2026-03-29 22:13:43,startIdx:4000000,endIdx:5000000,batch:5
2026-03-29 22:13:44,startIdx:5000000,endIdx:6000000,batch:6
2026-03-29 22:13:44,startIdx:6000000,endIdx:7000000,batch:7
2026-03-29 22:13:45,startIdx:7000000,endIdx:8000000,batch:8
2026-03-29 22:13:46,startIdx:8000000,endIdx:9000000,batch:9
2026-03-29 22:13:49,startIdx:9000000,endIdx:10000000,batch:10
2026-03-29 22:13:50,startIdx:10000000,endIdx:11000000,batch:11
2026-03-29 22:13:51,startIdx:11000000,endIdx:12000000,batch:12
2026-03-29 22:13:52,startIdx:12000000,endIdx:13000000,batch:13
2026-03-29 22:13:52,startIdx:13000000,endIdx:14000000,batch:14
2026-03-29 22:13:54,startIdx:14000000,endIdx:15000000,batch:15
2026-03-29 22:13:56,startIdx:15000000,endIdx:16000000,batch:16
2026-03-29 22:13:58,startIdx:16000000,endIdx:17000000,batch:17
2026-03-29 22:13:59,startIdx:17000000,endIdx:18000000,batch:18
2026-03-29 22:14:00,startIdx:18000000,endIdx:19000000,batch:19
2026-03-29 22:14:00,startIdx:19000000,endIdx:20000000,batch:20
2026-03-29 22:14:02,startIdx:20000000,endIdx:21000000,batch:21
2026-03-29 22:14:05,startIdx:21000000,endIdx:22000000,batch:22
2026-03-29 22:14:06,startIdx:22000000,endIdx:23000000,batch:23
2026-03-29 22:14:07,startIdx:23000000,endIdx:24000000,batch:24
2026-03-29 22:14:08,startIdx:24000000,endIdx:25000000,batch:25
2026-03-29 22:14:09,startIdx:25000000,endIdx:26000000,batch:26
2026-03-29 22:14:10,startIdx:26000000,endIdx:27000000,batch:27
2026-03-29 22:14:12,startIdx:27000000,endIdx:28000000,batch:28
2026-03-29 22:14:15,startIdx:28000000,endIdx:29000000,batch:29
2026-03-29 22:14:16,startIdx:29000000,endIdx:30000000,batch:30
2026-03-29 22:14:17,startIdx:30000000,endIdx:31000000,batch:31
2026-03-29 22:14:17,startIdx:31000000,endIdx:32000000,batch:32
2026-03-29 22:14:18,startIdx:32000000,endIdx:33000000,batch:33
2026-03-29 22:14:19,startIdx:33000000,endIdx:34000000,batch:34
2026-03-29 22:14:21,startIdx:34000000,endIdx:35000000,batch:35
2026-03-29 22:14:25,startIdx:35000000,endIdx:36000000,batch:36
2026-03-29 22:14:26,startIdx:36000000,endIdx:37000000,batch:37
2026-03-29 22:14:27,startIdx:37000000,endIdx:38000000,batch:38
2026-03-29 22:14:28,startIdx:38000000,endIdx:39000000,batch:39
2026-03-29 22:14:29,startIdx:39000000,endIdx:40000000,batch:40
2026-03-29 22:14:30,startIdx:40000000,endIdx:41000000,batch:41
2026-03-29 22:14:31,startIdx:41000000,endIdx:42000000,batch:42
2026-03-29 22:14:32,startIdx:42000000,endIdx:43000000,batch:43
2026-03-29 22:14:35,startIdx:43000000,endIdx:44000000,batch:44
2026-03-29 22:14:38,startIdx:44000000,endIdx:45000000,batch:45
2026-03-29 22:14:39,startIdx:45000000,endIdx:46000000,batch:46
2026-03-29 22:14:40,startIdx:46000000,endIdx:47000000,batch:47
2026-03-29 22:14:41,startIdx:47000000,endIdx:48000000,batch:48
2026-03-29 22:14:42,startIdx:48000000,endIdx:49000000,batch:49
2026-03-29 22:14:43,startIdx:49000000,endIdx:49999999,batch:50
2026-03-29 22:14:43,start:0,end:49999999,jsonFile:Json_202603292213367039.json finished

 

 

image

 

 

 

image

 

 

 

 

image

 

 

image

 

image

 

posted @ 2026-03-29 22:19  FredGrit  阅读(3)  评论(0)    收藏  举报