Advertisement
andrzejiwaniuk

[Napisz sam] Program do pisania ręcznego (np tablet) z anima

Jun 29th, 2013
540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.25 KB | None | 0 0
  1. //XAML
  2. <ComboBox x:Name="cboBrushSize" HorizontalAlignment="Left" Margin="568,195,0,0" VerticalAlignment="Top" Width="120" SelectionChanged="ComboBox_SelectionChanged">
  3.             <ComboBoxItem>1</ComboBoxItem>
  4.             <ComboBoxItem IsSelected="True">2</ComboBoxItem>
  5.             <ComboBoxItem>4</ComboBoxItem>
  6.             <ComboBoxItem>6</ComboBoxItem>
  7.             <ComboBoxItem>8</ComboBoxItem>
  8.             <ComboBoxItem>10</ComboBoxItem>
  9. </ComboBox>
  10.  
  11. //---------------------------
  12.  
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. using System.Windows.Controls;
  20. using System.Windows.Data;
  21. using System.Windows.Documents;
  22. using System.Windows.Input;
  23. using System.Windows.Media;
  24. using System.Windows.Media.Imaging;
  25. using System.Windows.Navigation;
  26. using System.Windows.Shapes;
  27. using System.IO;
  28. using Microsoft.Win32;
  29. using System.Windows.Forms;
  30. using System.Windows.Media.Animation;
  31.  
  32. namespace AndrzejIwaniuk
  33. {
  34.     /// <summary>
  35.     /// Eksport obrazka do formatu JPEG
  36.     /// Zmiana kolorów linii
  37.     /// Prosta animacja
  38.     /// </summary>
  39.    
  40.     public partial class MainWindow : Window
  41.     {
  42.        
  43.         string nazwaPliku = "";
  44.         static System.Windows.Media.Color col;
  45.         //static System.Drawing.Color col;
  46.         public MainWindow()
  47.         {
  48.             InitializeComponent();
  49.         }
  50.  
  51.         private void btnCzysc_Click(object sender, RoutedEventArgs e)
  52.         {
  53.             //czyść obszar pisania
  54.             recznePisanie.Strokes.Clear();
  55.            
  56.         }
  57.  
  58.         //zapisz obrazek jako plik JPG
  59.         public void EskportDoJPEG(InkCanvas obszar)
  60.         {
  61.             double
  62.                     x1 = obszar.Margin.Left,
  63.                     x2 = obszar.Margin.Top,
  64.                     x3 = obszar.Margin.Right,
  65.                     x4 = obszar.Margin.Bottom;
  66.  
  67.             obszar.Margin = new Thickness(0, 0, 0, 0);
  68.  
  69.             Size size = new Size(obszar.Width, obszar.Height);
  70.             obszar.Measure(size);
  71.             obszar.Arrange(new Rect(size));
  72.  
  73.             RenderTargetBitmap renderBitmap =
  74.              new RenderTargetBitmap(
  75.                (int)size.Width,
  76.                (int)size.Height,
  77.                96,
  78.                96,
  79.                PixelFormats.Default);
  80.             renderBitmap.Render(obszar);
  81.            
  82.             otworzOknoDialogoZapisz();
  83.  
  84.             try
  85.             {
  86.                 using (FileStream fs = File.Open(nazwaPliku, FileMode.Create))
  87.                 {
  88.                     JpegBitmapEncoder encoder = new JpegBitmapEncoder();
  89.                     encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
  90.                     encoder.Save(fs);
  91.                 }
  92.             }
  93.             catch (Exception e)
  94.             {
  95.             }
  96.             obszar.Margin = new Thickness(x1, x2, x3, x4);
  97.         }
  98.  
  99.         private void Button_Click(object sender, RoutedEventArgs e)
  100.         {
  101.             EskportDoJPEG(recznePisanie);
  102.         }
  103.         void otworzOknoDialogoZapisz()
  104.         {
  105.             //otworz save dialog box
  106.             // konfiguracja okna dialogowego Zapisu
  107.             Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  108.             dlg.FileName = "nazwa_pliku"; // domyślna nazwa pliku
  109.             dlg.DefaultExt = ".jpg"; // domyśle rozszerzenie pliku
  110.             dlg.Filter = "Obrazek (.jpg)|*.jpg"; // pokauj tylko pliku *.jpg
  111.  
  112.             // Pokaz okno dialogowe Zapisu
  113.             Nullable<bool> result = dlg.ShowDialog();
  114.  
  115.             // Sprawdź zmienna result z okna Dialogowe Zapis
  116.  
  117.             if (result == true)
  118.             {
  119.                 // Zapisanie dokumentu
  120.                 nazwaPliku = dlg.FileName;
  121.             }
  122.             //------
  123.         }
  124.         private void koloDialog()
  125.         //private System.Windows.Media.Color koloDialog()
  126.         {
  127.             ColorDialog colorDialog =
  128.                        new ColorDialog();
  129.             colorDialog.AllowFullOpen = true;
  130.             colorDialog.ShowDialog();
  131.             //col = colorDialog.Color;
  132.             col.A = colorDialog.Color.A;
  133.             col.B = colorDialog.Color.B;
  134.             col.G = colorDialog.Color.G;
  135.             col.R = colorDialog.Color.R;
  136.            
  137.         }
  138.      
  139.         private void btnKolory_Click(object sender, RoutedEventArgs e)
  140.         {
  141.             koloDialog();
  142.             ChangeColor(recznePisanie, col);
  143.         }
  144.  
  145.         void ChangeColor(InkCanvas inkCanvas, System.Windows.Media.Color color)
  146.         //void ChangeColor(InkCanvas inkCanvas, Color color)
  147.         {
  148.             //foreach (var stroke in inkCanvas.Strokes)
  149.             //{
  150.                 //stroke.DrawingAttributes.Color = Colors.Aqua;// col;
  151.  
  152.             inkCanvas.DefaultDrawingAttributes.Color = Color.FromArgb(col.A, col.R, col.G, col.B);  
  153.             //stroke.DrawingAttributes.Color = Color.FromArgb(col.A,col.R,col.G,col.B);
  154.                
  155.             //}
  156.            
  157.         }
  158.  
  159.         private void Window_Loaded(object sender, RoutedEventArgs e)
  160.         {
  161.             DoubleAnimation da = new DoubleAnimation
  162.               (360, 0, new Duration(TimeSpan.FromSeconds(3)));
  163.             RotateTransform rt = new RotateTransform();
  164.  
  165.             image1.RenderTransform = rt;
  166.             image1.RenderTransformOrigin = new Point(0.5, 0.5);
  167.             da.RepeatBehavior = RepeatBehavior.Forever;
  168.             rt.BeginAnimation(RotateTransform.AngleProperty, da);
  169.         }
  170.  
  171.         private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  172.         {
  173.             if (cboBrushSize.Items.Count > 0 && ((ComboBoxItem)cboBrushSize.SelectedItem).Content != null)
  174.             {
  175.                 /// Sets the brush size.
  176.                 recznePisanie.DefaultDrawingAttributes.Width =      Convert.ToDouble(((ComboBoxItem)cboBrushSize.SelectedItem).Content);
  177.                 recznePisanie.DefaultDrawingAttributes.Height = Convert.ToDouble(((ComboBoxItem)cboBrushSize.SelectedItem).Content);
  178.             }
  179.         }
  180.  
  181.         private void image1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  182.         {
  183.             recznePisanie.Background = Brushes.DarkSlateBlue;
  184.         }
  185.     }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement