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);
|
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 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()
|
private async Task LoadAsync()
|
||||||
{
|
{
|
||||||
@@ -27,7 +27,15 @@ public partial class AsyncImageRecordViewModel : ViewModelBase
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var bitmap = await Task.Run(() => new Bitmap(Path));
|
if (Data is not null)
|
||||||
|
{
|
||||||
|
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(() =>
|
await Dispatcher.UIThread.InvokeAsync(() =>
|
||||||
{
|
{
|
||||||
@@ -35,9 +43,13 @@ public partial class AsyncImageRecordViewModel : ViewModelBase
|
|||||||
IsLoading = false;
|
IsLoading = false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
else
|
||||||
|
{
|
||||||
|
Image = new WriteableBitmap(new Avalonia.PixelSize(1, 1), new Avalonia.Vector(96, 96), Avalonia.Platform.PixelFormat.Bgra8888, Avalonia.Platform.AlphaFormat.Premul);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch
|
||||||
{
|
{
|
||||||
Console.Error.WriteLine($"Ошибка во время загрузки изображения:\n{ex.Message}\n{ex.StackTrace}");
|
|
||||||
await Dispatcher.UIThread.InvokeAsync(() => IsLoading = false);
|
await Dispatcher.UIThread.InvokeAsync(() => IsLoading = false);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
|
|||||||
Reference in New Issue
Block a user