Xom9ik

Lab_5/15var (IV semester) CG

Apr 3rd, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.95 KB | None | 0 0
  1. .xaml
  2. <Window x:Class="TemplateWpfMaterial.MainWindow"
  3.         xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
  4.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  5.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  6.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8.         xmlns:local="clr-namespace:TemplateWpfMaterial"
  9.         mc:Ignorable="d"
  10.         Title=""
  11.         MinHeight="510"
  12.         MinWidth="600"
  13.         WindowStartupLocation="CenterScreen"
  14.         SizeToContent="WidthAndHeight"
  15.         WindowStyle="None"
  16.         WindowState="Normal"
  17.         ResizeMode="CanResizeWithGrip"
  18.         BorderThickness="1"
  19.         Foreground="{x:Null}"
  20.         BorderBrush="{x:Null}"
  21.         Background="{x:Null}"
  22.         Style="{StaticResource ModifiedWindow}" KeyDown="Window_KeyDown">
  23.     <Grid
  24.         Width="auto"
  25.         Height="auto"
  26.         x:Name="MainGrid"
  27.         Background="White">
  28.         <Grid.RowDefinitions>
  29.             <RowDefinition Height="30"  MinHeight="30" />
  30.             <RowDefinition Height="30"  MinHeight="30" />
  31.             <RowDefinition Height="30"  MinHeight="30" />
  32.             <RowDefinition Height="30"  MinHeight="30" />
  33.             <RowDefinition Height="30"  MinHeight="30" />
  34.             <RowDefinition Height="30"  MinHeight="30" />
  35.             <RowDefinition Height="30"  MinHeight="30" />
  36.             <RowDefinition Height="30"  MinHeight="30" />
  37.             <RowDefinition Height="30"  MinHeight="30" />
  38.             <RowDefinition Height="30*"  MinHeight="30" />
  39.  
  40.         </Grid.RowDefinitions>
  41.         <Grid.ColumnDefinitions>
  42.             <ColumnDefinition Width="170"  MinWidth="170" />
  43.             <ColumnDefinition Width="30*" MinWidth="30" />
  44.         </Grid.ColumnDefinitions>
  45.         <!--Up Panel-->
  46.         <StackPanel
  47.                 Grid.ColumnSpan="2"
  48.                 Grid.Column="0"
  49.                 x:Name="Drag"
  50.                 Grid.Row="0"
  51.                 HorizontalAlignment="Stretch"
  52.                 Height="30"
  53.                 Background="{DynamicResource PrimaryHueDarkBrush}"
  54.                 Margin="0,0,141,0" PreviewMouseDown="Drag_MouseDown">
  55.             <Label Content="Computer Graphics #5"
  56.                    HorizontalAlignment="Left"
  57.                    VerticalAlignment="Center"
  58.                    Margin="10,2,0,0"
  59.                    Foreground="{DynamicResource PrimaryHueMidForegroundBrush}"/>
  60.         </StackPanel>
  61.         <!--Close-->
  62.         <StackPanel
  63.                 Grid.ColumnSpan="2"
  64.                 Grid.Column="0"
  65.                 x:Name="Close"
  66.                 Width="47"
  67.                 Height="30"
  68.                 HorizontalAlignment="Right"
  69.             Margin="0,0,0,0"
  70.             MouseEnter="Close_MouseEnter"
  71.             MouseLeave="Close_MouseLeave"
  72.             Background="{DynamicResource PrimaryHueDarkBrush}"
  73.             PreviewMouseDown="Close_MouseDown">
  74.             <Image
  75.                 Width="19"
  76.                 Height="19"
  77.                 HorizontalAlignment="Center"
  78.                 Margin="0,5"
  79.             Source="pack://siteoforigin:,,,/Icon/Close.png" />
  80.         </StackPanel>
  81.         <!--Max-->
  82.         <StackPanel
  83.                 Grid.ColumnSpan="2"
  84.                 Grid.Column="0"
  85.                 x:Name="Max"
  86.                 Width="47"
  87.                 Height="30"
  88.                 Background="{DynamicResource PrimaryHueDarkBrush}"
  89.                 HorizontalAlignment="Right"
  90.                 Grid.Row="0"
  91.             Margin="0,0,47,0"
  92.             MouseEnter="Max_MouseEnter"
  93.             MouseLeave="Max_MouseLeave" PreviewMouseDown="Max_MouseDown">
  94.             <Image
  95.                 x:Name="IconMax"
  96.                 Width="11"
  97.                 Height="11"
  98.                 HorizontalAlignment="Center"
  99.                 Margin="0,9"
  100.             Source="pack://siteoforigin:,,,/Icon/Max.png" />
  101.         </StackPanel>
  102.         <!--Min-->
  103.         <StackPanel
  104.                 Grid.ColumnSpan="2"
  105.                 Grid.Column="0"
  106.                 x:Name="Min"
  107.                 Width="47"
  108.                 Height="30"
  109.                 HorizontalAlignment="Right"
  110.                 Grid.Row="0"
  111.             Margin="0,0,94,0"
  112.             Background="{DynamicResource PrimaryHueDarkBrush}"
  113.             MouseEnter="Min_MouseEnter"
  114.             MouseLeave="Min_MouseLeave"
  115.             PreviewMouseDown="Min_MouseDown">
  116.             <Image
  117.                 x:Name="IconMin"
  118.                Width="11"
  119.                 Height="11"
  120.                 HorizontalAlignment="Center"
  121.                 Margin="0,9"
  122.             Source="pack://siteoforigin:,,,/Icon/Min.png" />
  123.         </StackPanel>
  124.  
  125.         <Button
  126.             Margin="10,0,10,0"
  127.             Grid.Row="2"
  128.             Grid.Column="0"
  129.             Background="{DynamicResource PrimaryHueLightBrush}"
  130.             Content="Draw Image"
  131.             BorderBrush="{x:Null}" Click="Button_Click"
  132.             />
  133.         <Button
  134.             Margin="10,0,10,0"
  135.             Grid.Row="4"
  136.             Grid.Column="0"
  137.             Background="{DynamicResource PrimaryHueLightBrush}"
  138.             Content="Clear field"
  139.             BorderBrush="{x:Null}" Click="Button_Click_1"
  140.             />
  141.         <Button
  142.             Margin="10,0,10,0"
  143.             Grid.Row="6"
  144.             Grid.Column="0"
  145.             Background="{DynamicResource PrimaryHueLightBrush}"
  146.             Content="Display image"
  147.             BorderBrush="{x:Null}" Click="Button_Click_2"
  148.             />
  149.         <Button
  150.             Margin="10,0,10,0"
  151.             Grid.Row="8"
  152.             Grid.Column="0"
  153.             Background="{DynamicResource PrimaryHueLightBrush}"
  154.             Content="Convert to gray"
  155.             BorderBrush="{x:Null}" Click="Button_Click_3"
  156.             />
  157.         <StackPanel
  158.             Orientation="Horizontal"
  159.             Margin="0,0,10,10"
  160.             Grid.Row="2"
  161.             Grid.RowSpan="8"
  162.             Grid.Column="1"
  163.             Background="#FFFEFEFE">
  164.             <Canvas
  165.                 x:Name="Field">
  166.             </Canvas>
  167.             <Image
  168.                 x:Name="FieldImage">
  169.             </Image>
  170.         </StackPanel>
  171.  
  172.     </Grid>
  173. </Window>
  174.  
  175. .cs
  176. using System;
  177. using System.Windows;
  178. using System.Windows.Input;
  179. using System.Windows.Media;
  180. using System.Windows.Media.Imaging;
  181. using Microsoft.Win32;
  182.  
  183. namespace TemplateWpfMaterial
  184. {
  185.  
  186.     public partial class MainWindow : Window
  187.     {
  188.         public static BitmapImage IMax = new BitmapImage();
  189.         public static BitmapImage IMin = new BitmapImage();
  190.         public static BitmapImage IMaxBlack = new BitmapImage();
  191.         public static BitmapImage IMinBlack = new BitmapImage();
  192.  
  193.         public Brush PrimaryHueLightBrush = new SolidColorBrush();
  194.         public Brush PrimaryHueLightForegroundBrush = new SolidColorBrush();
  195.         public Brush PrimaryHueMidBrush = new SolidColorBrush();
  196.         public Brush PrimaryHueMidForegroundBrush = new SolidColorBrush();
  197.         public Brush PrimaryHueDarkBrush = new SolidColorBrush();
  198.         public Brush PrimaryHueDarkForegroundBrush = new SolidColorBrush();
  199.  
  200.         BrushConverter bc = new BrushConverter();
  201.         public MainWindow()
  202.         {
  203.             InitializeComponent();
  204.             IMax.BeginInit();
  205.             IMax.UriSource = new Uri("pack://siteoforigin:,,,/Icon/Max.png");
  206.             IMax.EndInit();
  207.             IMin.BeginInit();
  208.             IMin.UriSource = new Uri("pack://siteoforigin:,,,/Icon/Min.png");
  209.             IMin.EndInit();
  210.             IMaxBlack.BeginInit();
  211.             IMaxBlack.UriSource = new Uri("pack://siteoforigin:,,,/Icon/Max Black.png");
  212.             IMaxBlack.EndInit();
  213.             IMinBlack.BeginInit();
  214.             IMinBlack.UriSource = new Uri("pack://siteoforigin:,,,/Icon/Min Black.png");
  215.             IMinBlack.EndInit();
  216.             ResourceDictionary resources = this.Resources;
  217.             resources["PrimaryHueMidBrush"] = (Brush)bc.ConvertFrom("#3F51B5");
  218.             resources["PrimaryHueMidForegroundBrush"] = (Brush)bc.ConvertFrom("#FFFFFF");
  219.             resources["PrimaryHueDarkBrush"] = (Brush)bc.ConvertFrom("#303F9F");
  220.             resources["PrimaryHueDarkForegroundBrush"] = (Brush)bc.ConvertFrom("#FFFFFF");
  221.             resources["PrimaryHueLightBrush"] = (Brush)bc.ConvertFrom("#E91E63");
  222.             resources["PrimaryHueLightForegroundBrush"] = (Brush)bc.ConvertFrom("#FFFFFF");
  223.             this.MaxHeight = System.Windows.SystemParameters.WorkArea.Height + 10;
  224.             this.MaxWidth = System.Windows.SystemParameters.WorkArea.Width + 10;
  225.             GetColors();
  226.         }
  227.         public void GetColors()
  228.         {
  229.             var bc = new BrushConverter();
  230.             ResourceDictionary resources = this.Resources;
  231.             PrimaryHueLightBrush = (Brush)resources["PrimaryHueLightBrush"];
  232.             PrimaryHueLightForegroundBrush = (Brush)resources["PrimaryHueLightForegroundBrush"];
  233.             PrimaryHueMidBrush = (Brush)resources["PrimaryHueMidBrush"];
  234.             PrimaryHueMidForegroundBrush = (Brush)resources["PrimaryHueMidForegroundBrush"];
  235.             PrimaryHueDarkBrush = (Brush)resources["PrimaryHueDarkBrush"];
  236.             PrimaryHueDarkForegroundBrush = (Brush)resources["PrimaryHueDarkForegroundBrush"];
  237.             Min.Background = PrimaryHueDarkBrush;
  238.             Max.Background = PrimaryHueDarkBrush;
  239.             Close.Background = PrimaryHueDarkBrush;
  240.         }
  241.         private void Drag_MouseDown(object sender, MouseButtonEventArgs e)
  242.         {
  243.             if (WindowState == WindowState.Maximized)
  244.             {
  245.                 WindowState = WindowState.Normal;
  246.                 var point = e.GetPosition(this);
  247.                 Left = point.X - 10;
  248.                 Top = point.Y - 10;
  249.             }
  250.             this.DragMove();
  251.         }
  252.         private void Close_MouseEnter(object sender, MouseEventArgs e)
  253.         {
  254.             Close.Background = (Brush)bc.ConvertFrom("#FFE81123");
  255.         }
  256.  
  257.         private void Close_MouseLeave(object sender, MouseEventArgs e)
  258.         {
  259.             Close.Background = PrimaryHueDarkBrush;
  260.         }
  261.         private void Max_MouseEnter(object sender, MouseEventArgs e)
  262.         {
  263.             Max.Background = (Brush)bc.ConvertFrom("#FFe6e6e6");
  264.             IconMax.Source = IMaxBlack;
  265.         }
  266.         private void Max_MouseLeave(object sender, MouseEventArgs e)
  267.         {
  268.             IconMax.Source = IMax;
  269.             Max.Background = PrimaryHueDarkBrush;
  270.         }
  271.         private void Min_MouseEnter(object sender, MouseEventArgs e)
  272.         {
  273.             Min.Background = (Brush)bc.ConvertFrom("#FFe6e6e6");
  274.             IconMin.Source = IMinBlack;
  275.         }
  276.         private void Min_MouseLeave(object sender, MouseEventArgs e)
  277.         {
  278.             IconMin.Source = IMin;
  279.             Min.Background = PrimaryHueDarkBrush;
  280.         }
  281.         private void Close_MouseDown(object sender, MouseButtonEventArgs e)
  282.         {
  283.             Application.Current.Shutdown();
  284.         }
  285.         private void Max_MouseDown(object sender, MouseButtonEventArgs e)
  286.         {
  287.             Application.Current.MainWindow.SizeToContent = SizeToContent.Manual;
  288.             if (WindowState != WindowState.Maximized)
  289.                 Application.Current.MainWindow.WindowState = WindowState.Maximized;
  290.             else
  291.                 Application.Current.MainWindow.WindowState = WindowState.Normal;
  292.         }
  293.         private void Min_MouseDown(object sender, MouseButtonEventArgs e)
  294.         {
  295.             this.WindowState = System.Windows.WindowState.Minimized;
  296.         }
  297.         int x = 0, y = 0;
  298.         System.Windows.Shapes.Polygon p = new System.Windows.Shapes.Polygon();
  299.         System.Windows.Shapes.Polygon p2 = new System.Windows.Shapes.Polygon();
  300.  
  301.         public void Draw() {
  302.             Clear();
  303.             p.Stroke = Brushes.Black;
  304.             p.Fill = Brushes.LightBlue;
  305.             p.StrokeThickness = 1;
  306.             p.HorizontalAlignment = HorizontalAlignment.Left;
  307.             p.VerticalAlignment = VerticalAlignment.Center;
  308.             //
  309.             p.Points = new PointCollection() {
  310.                     new Point(38+x, 162+y),
  311.                     new Point(38+x,239+y),
  312.                     new Point(92+x,240+y),
  313.                     new Point(107+x,214+y),
  314.                     new Point(76+x,270+y),
  315.                     new Point(111+x,270+y),
  316.                     new Point(140+x,214+y),
  317.                     new Point(127+x,240+y),
  318.                     new Point(247+x,240+y),
  319.                     new Point(202+x,198+y),
  320.                     new Point(73+x, 198+y),
  321.                     new Point(63+x,161+y)
  322.             };
  323.             p2.Stroke = Brushes.Black;
  324.             p2.Fill = Brushes.LightBlue;
  325.             p2.StrokeThickness = 1;
  326.             p2.HorizontalAlignment = HorizontalAlignment.Left;
  327.             p2.VerticalAlignment = VerticalAlignment.Center;
  328.             //
  329.             p2.Points = new PointCollection() {
  330.                     new Point(202+x, 198+y),
  331.                     new Point(202+x,216+y),
  332.                     new Point(221+x,216+y)
  333.             };
  334.             Field.Children.Add(p);
  335.             Field.Children.Add(p2);
  336.         }
  337.         public void Clear()
  338.         {
  339.             Field.Children.Remove(p);
  340.             Field.Children.Remove(p2);
  341.             FieldImage.Source = null;
  342.         }
  343.         private void Button_Click(object sender, RoutedEventArgs e)
  344.         {
  345.             Draw();
  346.         }
  347.  
  348.         private void Button_Click_1(object sender, RoutedEventArgs e)
  349.         {
  350.             Clear();
  351.         }
  352.         BitmapImage image;
  353.         private void Button_Click_2(object sender, RoutedEventArgs e)
  354.         {
  355.             OpenFileDialog op = new OpenFileDialog();
  356.             op.Title = "Select a picture";
  357.             op.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" +
  358.               "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
  359.               "Portable Network Graphic (*.png)|*.png";
  360.             if (op.ShowDialog() == true)
  361.             {
  362.                 image = new BitmapImage(new Uri(op.FileName));
  363.                 FieldImage.Source = image;
  364.             }
  365.            
  366.         }
  367.  
  368.         private void Button_Click_3(object sender, RoutedEventArgs e)
  369.         {
  370.             var grayBitmapSource = new FormatConvertedBitmap();
  371.             grayBitmapSource.BeginInit();
  372.             grayBitmapSource.Source = image;
  373.             grayBitmapSource.DestinationFormat = PixelFormats.Gray32Float;
  374.             grayBitmapSource.EndInit();
  375.             FieldImage.Source = grayBitmapSource;
  376.         }
  377.  
  378.         private void Window_KeyDown(object sender, KeyEventArgs e)
  379.         {
  380.             if (e.Key == Key.Left)
  381.                 x -= 1;
  382.             else if (e.Key == Key.Up)
  383.                 y -= 1;
  384.             else if (e.Key == Key.Down)
  385.                 y += 1;
  386.             else if (e.Key == Key.Right)
  387.                 x += 1;
  388.             Draw();
  389.         }
  390.     }
  391. }
Advertisement
Add Comment
Please, Sign In to add comment