Skip to content

Commit

Permalink
add: 导入导出对比按钮的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
chenxuuu committed Sep 10, 2024
1 parent 108e3ba commit 2e8436d
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 18 deletions.
15 changes: 15 additions & 0 deletions Image2Display/Image2Display/Helpers/DialogHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,20 @@ public static async Task<IReadOnlyList<IStorageFile>> ShowOpenFileDialogAsync
};
return await ShowOpenFileDialogAsync(list, multiple);
}

public static async Task<string?> ShowSaveFileDialogAsync(string ext)
{
var options = new FilePickerSaveOptions
{
SuggestedFileName = "image",
FileTypeChoices = [ FilePickerFileTypes.ImagePng ]
};
var app = Application.Current!.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime;
var m = app!.MainWindow;
var file = await m!.StorageProvider.SaveFilePickerAsync(options);
if (file == null)
return null;
return file.Path.LocalPath;
}
}
}
54 changes: 54 additions & 0 deletions Image2Display/Image2Display/ViewModels/ImageProcessingViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using Avalonia.Media;
using Avalonia.Media.Imaging;
using Avalonia.Platform.Storage;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Image2Display.Helpers;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -93,5 +97,55 @@ public bool BinarizationThresholdShow
{
get => Binarization && QuantizationAlgorithm == 0;
}

//图片展示
[ObservableProperty]
private bool _isShowOriginalImage = false;
[ObservableProperty]
private Bitmap? _originalImage = null;
[ObservableProperty]
private Bitmap? _processedImage = null;

//开启一个定时器,用于更新显示原图的变量
private System.Timers.Timer ShowOriginalImageTimer = new();

[RelayCommand]
private void ShowOriginalImage()
{
IsShowOriginalImage = true;
ShowOriginalImageTimer.Stop();
ShowOriginalImageTimer.Start();
}

[RelayCommand]
private async Task OpenImageFile()
{
var files = await DialogHelper.ShowOpenFileDialogAsync(FilePickerFileTypes.ImageAll, false);
if (files.Count == 0)
return;
//读取图片,导入到OriginalImage内
OriginalImage = new Bitmap(Path.GetFullPath(files[0].Path.LocalPath));
ProcessedImage = new Bitmap(Path.GetFullPath(files[0].Path.LocalPath));
}

[RelayCommand]
private async Task SaveImageFile()
{
//保存图片
var path = await DialogHelper.ShowSaveFileDialogAsync("png");
if (path == null)
return;
ProcessedImage?.Save(path);
}

/// <summary>
/// 初始化ImageProcessingViewModel
/// </summary>
public ImageProcessingViewModel()
{
ShowOriginalImageTimer.Elapsed += (_,_) => IsShowOriginalImage = false;
ShowOriginalImageTimer.Interval = 50;
ShowOriginalImageTimer.AutoReset = false;
}
}
}
56 changes: 38 additions & 18 deletions Image2Display/Image2Display/Views/ImageProcessingView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,58 +18,78 @@
<!-- 图片框 -->
<Grid Grid.Row="0" Background="{StaticResource CheckerBoardBrush}">
<!-- 一堆按钮 -->
<StackPanel
Margin="10"
<Grid
Margin="0,0,10,10"
HorizontalAlignment="Right"
VerticalAlignment="Bottom"
Orientation="Vertical">
<Button
ColumnDefinitions="auto,auto"
RowDefinitions="auto,auto">
<RepeatButton
Grid.Row="1"
Grid.Column="0"
Width="40"
Height="40"
Margin="0,10,0,0"
Margin="10,10,0,0"
Padding="0"
Command="{Binding ShowOriginalImageCommand}"
Delay="0"
Interval="25"
ToolTip.Tip="{DynamicResource HoldToCompare}">
<ui:SymbolIcon FontSize="20" Symbol="Switch" />
</Button>
</RepeatButton>
<Button
Grid.Row="0"
Grid.Column="0"
Width="40"
Height="40"
Margin="0,10,0,0"
Margin="10,10,0,0"
Padding="0"
Command="{Binding OpenImageFileCommand}"
ToolTip.Tip="{DynamicResource LoadNewImage}">
<ui:SymbolIcon FontSize="20" Symbol="OpenFolder" />
</Button>
<Button
Grid.Row="0"
Grid.Column="1"
Width="40"
Height="40"
Margin="0,10,0,0"
Margin="10,10,0,0"
Padding="0"
Command="{Binding SaveImageFileCommand}"
ToolTip.Tip="{DynamicResource SaveAsNewFile}">
<ui:SymbolIcon FontSize="20" Symbol="Save" />
</Button>
<Button
Grid.Row="1"
Grid.Column="1"
Width="40"
Height="40"
Margin="0,10,0,0"
Margin="10,10,0,0"
Padding="0"
ToolTip.Tip="{DynamicResource PerformModulation}">
<ui:SymbolIcon FontSize="20" Symbol="Accept" />
</Button>
</StackPanel>
</Grid>
<!-- 空白图片 -->
<Border
Margin="10"
HorizontalAlignment="Center"
VerticalAlignment="Center"
BoxShadow="0 0 10 2 #7F7F7F7F">
<Viewbox>
<Image
Width="200"
Margin="100"
Opacity="0.5"
Source="/Assets/Images/unknowImage.png" />
</Viewbox>
<!-- 后面还要放两张图片,代表原图与处理后的图片 -->
<Grid>
<Viewbox IsVisible="{Binding OriginalImage, Converter={x:Static ObjectConverters.IsNull}}">
<Image
Width="200"
Margin="100"
Opacity="0.5"
Source="/Assets/Images/unknowImage.png" />
</Viewbox>
<!-- 后面还要放两张图片,代表原图与处理后的图片 -->
<Grid IsVisible="{Binding OriginalImage, Converter={x:Static ObjectConverters.IsNotNull}}">
<Image IsVisible="{Binding IsShowOriginalImage}" Source="{Binding OriginalImage}" />
<Image IsVisible="{Binding !IsShowOriginalImage}" Source="{Binding ProcessedImage}" />
</Grid>
</Grid>
</Border>
</Grid>
<ScrollViewer
Expand Down

0 comments on commit 2e8436d

Please sign in to comment.