forked from amkovkov/GranuSightSoftware2
feat: update async image view model
now accepting not path to image, but base64 encoded image data
This commit is contained in:
@@ -10,16 +10,16 @@ public partial class AsyncImageRecordViewModel : ViewModelBase
|
||||
{
|
||||
private static readonly SemaphoreSlim _semaphore = new(1, 1);
|
||||
|
||||
[ObservableProperty] public partial string Path { get; set; }
|
||||
[ObservableProperty] public partial string? Data { get; set; }
|
||||
[ObservableProperty] public partial IImage? Image { get; set; }
|
||||
[ObservableProperty] public partial bool IsLoading { get; set; } = true;
|
||||
[ObservableProperty] public partial bool IsLoading { get; set; } = false;
|
||||
|
||||
public AsyncImageRecordViewModel(string path)
|
||||
public AsyncImageRecordViewModel(string? data = null)
|
||||
{
|
||||
Path = path;
|
||||
Data = data;
|
||||
}
|
||||
|
||||
partial void OnPathChanged(string value) => _ = LoadAsync();
|
||||
partial void OnDataChanged(string? value) => _ = LoadAsync();
|
||||
|
||||
private async Task LoadAsync()
|
||||
{
|
||||
@@ -27,17 +27,29 @@ public partial class AsyncImageRecordViewModel : ViewModelBase
|
||||
|
||||
try
|
||||
{
|
||||
var bitmap = await Task.Run(() => new Bitmap(Path));
|
||||
|
||||
await Dispatcher.UIThread.InvokeAsync(() =>
|
||||
if (Data is not null)
|
||||
{
|
||||
Image = bitmap;
|
||||
IsLoading = false;
|
||||
});
|
||||
IsLoading = true;
|
||||
byte[] imageBytes = Convert.FromBase64String(Data);
|
||||
Bitmap bitmap;
|
||||
using (var stream = new MemoryStream(imageBytes))
|
||||
{
|
||||
bitmap = await Task.Run(() => new Bitmap(stream));
|
||||
}
|
||||
|
||||
await Dispatcher.UIThread.InvokeAsync(() =>
|
||||
{
|
||||
Image = bitmap;
|
||||
IsLoading = false;
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
Image = new WriteableBitmap(new Avalonia.PixelSize(1, 1), new Avalonia.Vector(96, 96), Avalonia.Platform.PixelFormat.Bgra8888, Avalonia.Platform.AlphaFormat.Premul);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
catch
|
||||
{
|
||||
Console.Error.WriteLine($"Ошибка во время загрузки изображения:\n{ex.Message}\n{ex.StackTrace}");
|
||||
await Dispatcher.UIThread.InvokeAsync(() => IsLoading = false);
|
||||
}
|
||||
finally
|
||||
|
||||
Reference in New Issue
Block a user