Skip to content

Commit 3bf4e7c

Browse files
authored
Merge pull request #205 from Freeesia/copilot/fix-file-handle-issue
画像読み込み時のファイルハンドル保持問題を修正
2 parents 1b1c6bc + 06472b7 commit 3bf4e7c

4 files changed

Lines changed: 45 additions & 4 deletions

File tree

VdLabel/DesktopCatalog.xaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
WindowBackdropType="Mica"
2626
WindowCornerPreference="Default"
2727
mc:Ignorable="d">
28+
<ui:FluentWindow.Resources>
29+
<local:FilePathToImageSourceConverter x:Key="filePathToImageConv" />
30+
</ui:FluentWindow.Resources>
2831
<ui:FluentWindow.InputBindings>
2932
<KeyBinding Key="Esc" Command="ApplicationCommands.Close" />
3033
<KeyBinding Key="Enter" Command="ApplicationCommands.Close" />
@@ -58,7 +61,7 @@
5861
<ui:Image
5962
CornerRadius="8"
6063
Focusable="False"
61-
Source="{Binding ImagePath}" />
64+
Source="{Binding ImagePath, Converter={StaticResource filePathToImageConv}}" />
6265
<ItemsControl
6366
Margin="6"
6467
HorizontalAlignment="Left"

VdLabel/MainWindow.xaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<Setter Property="MinWidth" Value="80" />
3636
</Style>
3737
<BooleanToVisibilityConverter x:Key="b2vConv" />
38+
<local:FilePathToImageSourceConverter x:Key="filePathToImageConv" />
3839
</ui:FluentWindow.Resources>
3940
<Grid>
4041
<DockPanel>
@@ -263,7 +264,7 @@
263264
<Image
264265
Height="16"
265266
DockPanel.Dock="Left"
266-
Source="{Binding ImagePath}" />
267+
Source="{Binding ImagePath, Converter={StaticResource filePathToImageConv}}" />
267268
<emoji:TextBlock
268269
Margin="4,0"
269270
Text="{Binding Title}"
@@ -399,7 +400,7 @@
399400
Grid.ColumnSpan="2"
400401
Height="120"
401402
CornerRadius="4"
402-
Source="{Binding ImagePath}"
403+
Source="{Binding ImagePath, Converter={StaticResource filePathToImageConv}}"
403404
Visibility="{Binding IsVisibleImage, Converter={StaticResource b2vConv}}" />
404405
<Label
405406
Grid.Row="8"

VdLabel/OverlayWindow.xaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
mc:Ignorable="d">
2323
<Window.Resources>
2424
<BooleanToVisibilityConverter x:Key="b2vConv" />
25+
<local:FilePathToImageSourceConverter x:Key="filePathToImageConv" />
2526
</Window.Resources>
2627
<Grid
2728
Width="{Binding OverlaySize, Mode=OneWay}"
@@ -73,7 +74,7 @@
7374
<Grid>
7475
<ui:Image
7576
CornerRadius="8"
76-
Source="{Binding ImagePath}"
77+
Source="{Binding ImagePath, Converter={StaticResource filePathToImageConv}}"
7778
Visibility="{Binding IsVisibleImage, Converter={StaticResource b2vConv}}" />
7879
<ItemsControl
7980
Margin="12"

VdLabel/OverlayWindow.xaml.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,42 @@ private void TopMost()
5353
}
5454
}
5555

56+
/// <summary>
57+
/// ファイルパス文字列を ImageSource に変換するコンバーター。
58+
/// BitmapCacheOption.OnLoad を使用することで、読み込み後にファイルハンドルを解放する。
59+
/// </summary>
60+
[ValueConversion(typeof(string), typeof(System.Windows.Media.Imaging.BitmapImage))]
61+
public sealed class FilePathToImageSourceConverter : IValueConverter
62+
{
63+
public static FilePathToImageSourceConverter Default { get; } = new FilePathToImageSourceConverter();
64+
65+
public object? Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
66+
{
67+
if (value is not string path || string.IsNullOrEmpty(path) || !System.IO.File.Exists(path))
68+
{
69+
return null;
70+
}
71+
72+
try
73+
{
74+
var bitmap = new System.Windows.Media.Imaging.BitmapImage();
75+
bitmap.BeginInit();
76+
bitmap.UriSource = new Uri(path, UriKind.Absolute);
77+
bitmap.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad;
78+
bitmap.EndInit();
79+
bitmap.Freeze();
80+
return bitmap;
81+
}
82+
catch
83+
{
84+
return null;
85+
}
86+
}
87+
88+
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
89+
=> throw new NotSupportedException();
90+
}
91+
5692
[ValueConversion(typeof(System.Drawing.Color), typeof(SolidColorBrush))]
5793
public sealed class SystemColorToSolidBrushConverter : IValueConverter
5894
{

0 commit comments

Comments
 (0)