1- using OnnxStack . UI . Commands ;
1+ using OnnxStack . Core ;
2+ using OnnxStack . UI . Commands ;
23using OnnxStack . UI . Dialogs ;
34using OnnxStack . UI . Models ;
45using OnnxStack . UI . Services ;
6+ using System ;
57using System . ComponentModel ;
8+ using System . Linq ;
69using System . Runtime . CompilerServices ;
710using System . Threading . Tasks ;
811using System . Windows ;
912using System . Windows . Controls ;
1013using System . Windows . Ink ;
14+ using System . Windows . Input ;
1115using System . Windows . Media ;
1216using System . Windows . Media . Imaging ;
1317
@@ -36,14 +40,17 @@ public ImageInputControl()
3640 ClearImageCommand = new AsyncRelayCommand ( ClearImage ) ;
3741 MaskModeCommand = new AsyncRelayCommand ( MaskMode ) ;
3842 SaveMaskCommand = new AsyncRelayCommand ( SaveMask ) ;
43+ CopyImageCommand = new AsyncRelayCommand ( CopyImage ) ;
44+ PasteImageCommand = new AsyncRelayCommand ( PasteImage ) ;
3945 InitializeComponent ( ) ;
4046 }
4147
4248 public AsyncRelayCommand LoadImageCommand { get ; }
4349 public AsyncRelayCommand ClearImageCommand { get ; }
4450 public AsyncRelayCommand MaskModeCommand { get ; }
4551 public AsyncRelayCommand SaveMaskCommand { get ; }
46-
52+ public AsyncRelayCommand CopyImageCommand { get ; }
53+ public AsyncRelayCommand PasteImageCommand { get ; }
4754 public ImageInput Result
4855 {
4956 get { return ( ImageInput ) GetValue ( ResultProperty ) ; }
@@ -146,18 +153,7 @@ public int MaskDrawSize
146153 /// <returns></returns>
147154 private Task LoadImage ( )
148155 {
149- var loadImageDialog = _dialogService . GetDialog < CropImageDialog > ( ) ;
150- loadImageDialog . Initialize ( SchedulerOptions . Width , SchedulerOptions . Height ) ;
151- if ( loadImageDialog . ShowDialog ( ) == true )
152- {
153- ClearImage ( ) ;
154- Result = new ImageInput
155- {
156- Image = loadImageDialog . GetImageResult ( ) ,
157- FileName = loadImageDialog . ImageFile ,
158- } ;
159- HasResult = true ;
160- }
156+ ShowCropImageDialog ( ) ;
161157 return Task . CompletedTask ;
162158 }
163159
@@ -260,6 +256,70 @@ public BitmapSource CreateMaskImage()
260256 return renderBitmap ;
261257 }
262258
259+ private void ShowCropImageDialog ( BitmapSource source = null , string sourceFile = null )
260+ {
261+ try
262+ {
263+ if ( ! string . IsNullOrEmpty ( sourceFile ) )
264+ source = new BitmapImage ( new Uri ( sourceFile ) ) ;
265+ }
266+ catch { }
267+
268+ var loadImageDialog = _dialogService . GetDialog < CropImageDialog > ( ) ;
269+ loadImageDialog . Initialize ( SchedulerOptions . Width , SchedulerOptions . Height , source ) ;
270+ if ( loadImageDialog . ShowDialog ( ) == true )
271+ {
272+ ClearImage ( ) ;
273+ Result = new ImageInput
274+ {
275+ Image = loadImageDialog . GetImageResult ( ) ,
276+ FileName = loadImageDialog . ImageFile ,
277+ } ;
278+ HasResult = true ;
279+ }
280+ }
281+
282+
283+ /// <summary>
284+ /// Copies the image.
285+ /// </summary>
286+ /// <returns></returns>
287+ private Task CopyImage ( )
288+ {
289+ if ( Result ? . Image != null )
290+ Clipboard . SetImage ( Result . Image ) ;
291+ return Task . CompletedTask ;
292+ }
293+
294+
295+ /// <summary>
296+ /// Paste the image.
297+ /// </summary>
298+ /// <returns></returns>
299+ private Task PasteImage ( )
300+ {
301+ return HandleClipboardInput ( ) ;
302+ }
303+
304+
305+ /// <summary>
306+ /// Handles the clipboard input.
307+ /// </summary>
308+ /// <returns></returns>
309+ private Task HandleClipboardInput ( )
310+ {
311+ if ( Clipboard . ContainsImage ( ) )
312+ ShowCropImageDialog ( Clipboard . GetImage ( ) ) ;
313+ else if ( Clipboard . ContainsFileDropList ( ) )
314+ {
315+ var imageFile = Clipboard . GetFileDropList ( )
316+ . OfType < string > ( )
317+ . FirstOrDefault ( ) ;
318+ ShowCropImageDialog ( null , imageFile ) ;
319+ }
320+ return Task . CompletedTask ;
321+ }
322+
263323
264324 /// <summary>
265325 /// Handles the MouseLeftButtonDown event of the MaskCanvas control.
@@ -272,15 +332,56 @@ private void MaskCanvas_MouseLeftButtonDown(object sender, System.Windows.Input.
272332 HasMaskChanged = true ;
273333 }
274334
335+
336+ /// <summary>
337+ /// Called on key down.
338+ /// </summary>
339+ /// <param name="sender">The sender.</param>
340+ /// <param name="e">The <see cref="KeyEventArgs"/> instance containing the event data.</param>
341+ private async void OnPreviewKeyDown ( object sender , KeyEventArgs e )
342+ {
343+ if ( e . Key == Key . V && Keyboard . Modifiers == ModifierKeys . Control )
344+ {
345+ await HandleClipboardInput ( ) ;
346+ e . Handled = true ;
347+ }
348+ else if ( e . Key == Key . C && Keyboard . Modifiers == ModifierKeys . Control )
349+ {
350+ await CopyImage ( ) ;
351+ e . Handled = true ;
352+ }
353+ }
354+
355+
356+ /// <summary>
357+ /// Called when mouse enters.
358+ /// </summary>
359+ /// <param name="sender">The sender.</param>
360+ /// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
361+ private void OnMouseEnter ( object sender , MouseEventArgs e )
362+ {
363+ Focus ( ) ;
364+ }
365+
366+
367+ /// <summary>
368+ /// Called when [preview drop].
369+ /// </summary>
370+ /// <param name="sender">The sender.</param>
371+ /// <param name="e">The <see cref="DragEventArgs"/> instance containing the event data.</param>
372+ private void OnPreviewDrop ( object sender , DragEventArgs e )
373+ {
374+ var fileNames = ( string [ ] ) e . Data . GetData ( DataFormats . FileDrop ) ;
375+ if ( ! fileNames . IsNullOrEmpty ( ) )
376+ ShowCropImageDialog ( null , fileNames . FirstOrDefault ( ) ) ;
377+ }
378+
275379 #region INotifyPropertyChanged
276380 public event PropertyChangedEventHandler PropertyChanged ;
277381 public void NotifyPropertyChanged ( [ CallerMemberName ] string property = "" )
278382 {
279383 PropertyChanged ? . Invoke ( this , new PropertyChangedEventArgs ( property ) ) ;
280384 }
281-
282385 #endregion
283-
284-
285386 }
286387}
0 commit comments