Skip to content
This repository has been archived by the owner on Jun 8, 2022. It is now read-only.

Commit

Permalink
cleanup UI
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSwerik committed May 8, 2020
1 parent 30d5d12 commit 9585e67
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 42 deletions.
3 changes: 2 additions & 1 deletion RedEye.UI/src/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:RedEye"
StartupUri="src/MainWindow.xaml">
StartupUri="MainWindow.xaml">
<Application.Resources>

<local:BoolToGridRowHeightConverter x:Key="BoolToGridRowHeightConverter" />
</Application.Resources>
</Application>
21 changes: 21 additions & 0 deletions RedEye.UI/src/BoolToGridRowHeightConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace RedEye
{
[ValueConversion(typeof(bool), typeof(GridLength))]
public class BoolToGridRowHeightConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((bool)value == true) ? new GridLength(1, GridUnitType.Star) : new GridLength(0);
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{ // Don't need any convert back
return null!;
}
}
}
12 changes: 7 additions & 5 deletions RedEye.UI/src/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:RedEye"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" Closed="Window_OnClosed" SizeChanged="Window_OnSizeChanged">
Title="RedEye" Height="450" Width="800" Closed="Window_OnClosed" SizeChanged="Window_OnSizeChanged"
d:DataContext="{d:DesignInstance }">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
Expand All @@ -15,9 +16,9 @@
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" x:Name="DeviceComboRow" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" x:Name="NextButtonRow" />
<RowDefinition Height="Auto" />
<RowDefinition Height="1.25*" />
</Grid.RowDefinitions>
Expand All @@ -32,7 +33,8 @@
<RadioButton Grid.Column="2" Content="Camera" x:Name="RadioButtonCam" Checked="RadioButtonCamera_OnChecked" />
</Grid>

<ComboBox Grid.Column="1" Grid.Row="2" Text="Device:" x:Name="DeviceBox" SelectionChanged="DeviceBox_OnSelectionChanged" />
<ComboBox Grid.Column="1" Grid.Row="2" Text="Device:" x:Name="DeviceBox"
SelectionChanged="DeviceBox_OnSelectionChanged" />

<Canvas Grid.Column="1" Grid.Row="3" x:Name="MainCanvas" IsHitTestVisible="False" ClipToBounds="True"
Width="{Binding ActualWidth, ElementName=Pic, Mode=OneWay}">
Expand All @@ -41,7 +43,7 @@
</Canvas>

<Button Grid.Column="1" Grid.Row="4" Content="Next" x:Name="NextButton" Click="NextButton_OnClick" />

<Button Grid.Column="1" Grid.Row="5" Content="Save" x:Name="SaveButton" Click="SaveButton_OnClick" />
</Grid>
</Window>
73 changes: 37 additions & 36 deletions RedEye.UI/src/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ private void RadioButtonCamera_OnChecked(object sender, RoutedEventArgs e)
DeviceBox_OnSelectionChanged(null, null);
NextButton.Visibility = Visibility.Hidden;
DeviceBox.Visibility = Visibility.Visible;
NextButtonRow.Height = new GridLength(0);
DeviceComboRow.Height = GridLength.Auto;
}

private void DeviceBox_OnSelectionChanged(object? sender, SelectionChangedEventArgs? e)
Expand All @@ -84,42 +86,7 @@ private void Window_OnSizeChanged(object sender, SizeChangedEventArgs e)
);
}

private void SaveButton_OnClick(object sender, RoutedEventArgs e)
{
Dispatcher.BeginInvoke((Action) SavePNG, DispatcherPriority.ContextIdle);
}

private void SavePNG()
{
var bounds = VisualTreeHelper.GetDescendantBounds(MainCanvas);
var rtb = new RenderTargetBitmap((int) bounds.Width, (int) bounds.Height, 96d, 96d, PixelFormats.Default);

DrawingVisual dv = new DrawingVisual();
using (DrawingContext dc = dv.RenderOpen())
{
VisualBrush vb = new VisualBrush(MainCanvas);
dc.DrawRectangle(vb, null, new Rect(new Point(), bounds.Size));
}

rtb.Render(dv);
var pngEncoder = new PngBitmapEncoder();
pngEncoder.Frames.Add(BitmapFrame.Create(rtb));

try
{
var ms = new MemoryStream();
pngEncoder.Save(ms);
ms.Close();
File.WriteAllBytes(
Config.Get("ScreenshotLocation") + $@"\RedEye {DateTime.Now:yyyy-MM-dd hh-mm-ss}.png",
ms.ToArray());
}
catch (Exception err)
{
const string message = "Failed to Save Image:\n";
MessageBox.Show(message + err.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void SaveButton_OnClick(object sender, RoutedEventArgs e) => Dispatcher.BeginInvoke((Action) SavePNG, DispatcherPriority.ContextIdle);

// Helper Methods:
private void ClearCanvas()
Expand Down Expand Up @@ -170,12 +137,46 @@ private void SwitchToImage()
NextButton_OnClick(null, null);
NextButton.Visibility = Visibility.Visible;
DeviceBox.Visibility = Visibility.Hidden;
DeviceComboRow.Height = new GridLength(0);
NextButtonRow.Height = GridLength.Auto;
}

private void DetectAsync()
{
if (!Config.IsCudaEnabled) DrawDetection(new Mat(_images.CurrentImagePath()).ToImage<Gray, byte>());
else DrawDetection(new GpuMat(new Mat(_images.CurrentImagePath()).ToImage<Gray, byte>()));
}

private void SavePNG()
{
var bounds = VisualTreeHelper.GetDescendantBounds(MainCanvas);
var rtb = new RenderTargetBitmap((int) bounds.Width, (int) bounds.Height, 96d, 96d, PixelFormats.Default);

DrawingVisual dv = new DrawingVisual();
using (DrawingContext dc = dv.RenderOpen())
{
VisualBrush vb = new VisualBrush(MainCanvas);
dc.DrawRectangle(vb, null, new Rect(new Point(), bounds.Size));
}

rtb.Render(dv);
var pngEncoder = new PngBitmapEncoder();
pngEncoder.Frames.Add(BitmapFrame.Create(rtb));

try
{
var ms = new MemoryStream();
pngEncoder.Save(ms);
ms.Close();
File.WriteAllBytes(
Config.Get("ScreenshotLocation") + $@"\RedEye {DateTime.Now:yyyy-MM-dd hh-mm-ss}.png",
ms.ToArray());
}
catch (Exception err)
{
const string message = "Failed to Save Image:\n";
MessageBox.Show(message + err.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}

0 comments on commit 9585e67

Please sign in to comment.