Files
annotations/Azaion.Annotator/DTO/ThumbnailDto.cs
T
2024-09-04 20:02:55 +03:00

57 lines
1.5 KiB
C#

using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Media.Imaging;
namespace Azaion.Annotator.DTO;
public class ThumbnailDto : INotifyPropertyChanged
{
public string ThumbnailPath { get; set; }
public string ImagePath { get; set; }
public string LabelPath { get; set; }
private BitmapImage? _image;
public BitmapImage? Image
{
get
{
if (_image == null)
LoadImageAsync();
return _image;
}
set
{
_image = value;
OnPropertyChanged();
}
}
private async void LoadImageAsync()
{
await Task.Run(() =>
{
try
{
var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(ThumbnailPath);
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.DecodePixelWidth = 480;
bitmap.DecodePixelHeight = 270;
bitmap.EndInit();
bitmap.Freeze(); // Freeze to make it cross-thread accessible
Image = bitmap;
}
catch (Exception e)
{
Console.WriteLine(e);
}
});
}
public event PropertyChangedEventHandler? PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}