Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion VdLabel/DesktopCatalog.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
WindowBackdropType="Mica"
WindowCornerPreference="Default"
mc:Ignorable="d">
<ui:FluentWindow.Resources>
<local:FilePathToImageSourceConverter x:Key="filePathToImageConv" />
</ui:FluentWindow.Resources>
<ui:FluentWindow.InputBindings>
<KeyBinding Key="Esc" Command="ApplicationCommands.Close" />
<KeyBinding Key="Enter" Command="ApplicationCommands.Close" />
Expand Down Expand Up @@ -58,7 +61,7 @@
<ui:Image
CornerRadius="8"
Focusable="False"
Source="{Binding ImagePath}" />
Source="{Binding ImagePath, Converter={StaticResource filePathToImageConv}}" />
<ItemsControl
Margin="6"
HorizontalAlignment="Left"
Expand Down
5 changes: 3 additions & 2 deletions VdLabel/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<Setter Property="MinWidth" Value="80" />
</Style>
<BooleanToVisibilityConverter x:Key="b2vConv" />
<local:FilePathToImageSourceConverter x:Key="filePathToImageConv" />
</ui:FluentWindow.Resources>
<Grid>
<DockPanel>
Expand Down Expand Up @@ -263,7 +264,7 @@
<Image
Height="16"
DockPanel.Dock="Left"
Source="{Binding ImagePath}" />
Source="{Binding ImagePath, Converter={StaticResource filePathToImageConv}}" />
<emoji:TextBlock
Margin="4,0"
Text="{Binding Title}"
Expand Down Expand Up @@ -399,7 +400,7 @@
Grid.ColumnSpan="2"
Height="120"
CornerRadius="4"
Source="{Binding ImagePath}"
Source="{Binding ImagePath, Converter={StaticResource filePathToImageConv}}"
Visibility="{Binding IsVisibleImage, Converter={StaticResource b2vConv}}" />
<Label
Grid.Row="8"
Expand Down
3 changes: 2 additions & 1 deletion VdLabel/OverlayWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
mc:Ignorable="d">
<Window.Resources>
<BooleanToVisibilityConverter x:Key="b2vConv" />
<local:FilePathToImageSourceConverter x:Key="filePathToImageConv" />
</Window.Resources>
<Grid
Width="{Binding OverlaySize, Mode=OneWay}"
Expand Down Expand Up @@ -73,7 +74,7 @@
<Grid>
<ui:Image
CornerRadius="8"
Source="{Binding ImagePath}"
Source="{Binding ImagePath, Converter={StaticResource filePathToImageConv}}"
Visibility="{Binding IsVisibleImage, Converter={StaticResource b2vConv}}" />
<ItemsControl
Margin="12"
Expand Down
36 changes: 36 additions & 0 deletions VdLabel/OverlayWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,42 @@ private void TopMost()
}
}

/// <summary>
/// ファイルパス文字列を ImageSource に変換するコンバーター。
/// BitmapCacheOption.OnLoad を使用することで、読み込み後にファイルハンドルを解放する。
/// </summary>
[ValueConversion(typeof(string), typeof(System.Windows.Media.Imaging.BitmapImage))]
public sealed class FilePathToImageSourceConverter : IValueConverter
{
public static FilePathToImageSourceConverter Default { get; } = new FilePathToImageSourceConverter();

public object? Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is not string path || string.IsNullOrEmpty(path) || !System.IO.File.Exists(path))
{
return null;
}

try
{
var bitmap = new System.Windows.Media.Imaging.BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(path, UriKind.Absolute);
bitmap.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad;
bitmap.EndInit();
bitmap.Freeze();
return bitmap;
}
catch
{
return null;
}
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
=> throw new NotSupportedException();
}

[ValueConversion(typeof(System.Drawing.Color), typeof(SolidColorBrush))]
public sealed class SystemColorToSolidBrushConverter : IValueConverter
{
Expand Down