Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Drawing.Imaging;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17.  
  18.  
  19. namespace aiextractor
  20. {
  21. /// <summary>
  22. /// Interaction logic for MainWindow.xaml
  23. /// </summary>
  24. public partial class MainWindow : Window
  25. {
  26. int OriginalXSize { get; set; }
  27. int OriginalYSize { get; set; }
  28. double ScaledX { get; set; }
  29. double ScaledY { get; set; }
  30. double toCropBeginX { get; set; }
  31. double toCropBeginY { get; set; }
  32. double toCropEndX { get; set; }
  33. double toCropEndY { get; set; }
  34. Bitmap temp;
  35.  
  36. public MainWindow()
  37. {
  38. InitializeComponent();
  39. }
  40.  
  41. private void btnLoad_Click(object sender, RoutedEventArgs e)
  42. {
  43. OpenFileDialog op = new OpenFileDialog();
  44. op.Title = "Select a picture";
  45. op.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" +
  46. "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
  47. "Portable Network Graphic (*.png)|*.png";
  48. if (op.ShowDialog() == true)
  49. {
  50. imgPhoto.Source = new BitmapImage(new Uri(op.FileName));
  51. temp = new Bitmap(op.FileName);
  52. OriginalXSize = ((System.Windows.Media.Imaging.BitmapSource)imgPhoto.Source).PixelWidth;
  53. OriginalYSize = ((System.Windows.Media.Imaging.BitmapSource)imgPhoto.Source).PixelHeight;
  54. }
  55.  
  56. }
  57.  
  58. private void btnSave_Click(object sender, RoutedEventArgs e)
  59. {
  60.  
  61.  
  62.  
  63. }
  64.  
  65. System.Windows.Point startPos;
  66. private void ImgPhoto_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  67. {
  68. startPos = e.GetPosition(imgPhoto);
  69. }
  70.  
  71. private void ImgPhoto_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  72. {
  73. ScaledX = OriginalXSize / imgPhoto.ActualWidth;
  74. ScaledY = OriginalYSize / imgPhoto.ActualHeight;
  75. var pos = e.GetPosition(imgPhoto);
  76.  
  77. toCropBeginX = pos.X * ScaledX;
  78. toCropBeginY = pos.Y * ScaledY;
  79.  
  80. rect.Margin = new Thickness(startPos.X, startPos.Y, 0, 0);
  81. rect.HorizontalAlignment = HorizontalAlignment.Left;
  82. rect.VerticalAlignment = VerticalAlignment.Top;
  83.  
  84. var selectedXs = Math.Abs(pos.X - startPos.X);
  85. var selectedYs = Math.Abs(pos.Y - startPos.Y);
  86.  
  87. if (selectedXs > selectedYs)
  88. {
  89. rect.Width = selectedXs;
  90. rect.Height = selectedXs;
  91. }
  92. else
  93. {
  94. rect.Width = selectedYs;
  95. rect.Height = selectedYs;
  96. }
  97.  
  98. toCropEndX = rect.Width * ScaledX;
  99. toCropEndY = rect.Height * ScaledY; // old toCropBeginY + (rect.Width * ScaledY);
  100.  
  101. Rectangle destinationRect = new Rectangle((int)toCropBeginX, (int)toCropBeginY, (int)toCropEndX, (int)toCropEndY);
  102. Bitmap croppedImage = temp.Clone(destinationRect, temp.PixelFormat);
  103. croppedImage.Save(@"test.png", ImageFormat.Png);
  104.  
  105.  
  106. rect.Fill = new SolidColorBrush(Colors.Red);
  107. rect.Opacity = 0.25;
  108. }
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement