/InstantImageUploader/Views/CaptureControl.xaml.cs
C# | 85 lines | 71 code | 11 blank | 3 comment | 6 complexity | 709df487958014aead5eb7f3a3ccf59c MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Shapes;
- using System.Drawing;
- using System.Threading.Tasks;
- using InstantImageUploader.ViewModels;
-
- namespace InstantImageUploader.Views
- {
- /// <summary>
- /// CaptureWindow.xaml の相互作用ロジック
- /// </summary>
- public partial class CaptureControl : Window
- {
- public CaptureControl()
- {
- InitializeComponent();
- }
-
- private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- var position = e.GetPosition(this);
-
- Canvas.SetLeft(target, position.X);
- Canvas.SetTop(target, position.Y);
- }
-
- private void Window_MouseMove(object sender, MouseEventArgs e)
- {
- if (e.LeftButton == MouseButtonState.Pressed)
- {
- var position = e.GetPosition(this);
-
- var width = position.X - Canvas.GetLeft(target);
- var height = position.Y - Canvas.GetTop(target);
-
- if (width > 0 && height > 0)
- {
- target.Width = width;
- target.Height = height;
- }
- }
- }
-
- private void Window_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
- {
- if (target.Width < 10) return;
- if (target.Height < 10) return;
-
- target.Visibility = Visibility.Collapsed;
- Cursor = Cursors.Arrow;
-
- Task.Factory.StartNew(() =>
- {
- System.Threading.Thread.Sleep(500);
-
- Dispatcher.Invoke((Action)(() =>
- {
- using (var screenBmp = new Bitmap(
- (int)target.Width,
- (int)target.Height))
- {
- using (var bmpGraphics = Graphics.FromImage(screenBmp))
- {
- bmpGraphics.CopyFromScreen((int)Canvas.GetLeft(target)-5, (int)Canvas.GetTop(target)-5, 0, 0, screenBmp.Size);
-
- var tempFilePath = System.IO.Path.GetTempFileName();
- screenBmp.Save(tempFilePath, System.Drawing.Imaging.ImageFormat.Png);
- ((CaptureWindowViewModel)DataContext).SendImageCommand.Execute(tempFilePath);
- }
- }
- }), null);
- });
- }
- }
- }