Advertisement
Nakumas

MergeR src code

Apr 27th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Forms;
  15. using System.Windows.Input;
  16. using System.Windows.Media;
  17. using System.Windows.Media.Imaging;
  18. using System.Windows.Navigation;
  19. using System.Windows.Shapes;
  20.  
  21. namespace MergeR_BGWorker
  22. {
  23.     /// <summary>
  24.     /// Logika interakcji dla klasy MainWindow.xaml
  25.     /// </summary>
  26.     public partial class MainWindow : Window
  27.     {
  28.         #region Prop init.
  29.         private BackgroundWorker bgw = null;
  30.         public string MainPath { get; set; }
  31.         public string OutputFilePath { get; set; }
  32.         public string TilesDirectoryName { get; set; }
  33.         public string TileFileName { get; set; }
  34.         public int DirectoriesCount { get; set; }
  35.         public int TilesCount { get; set; }
  36.         public int TileWidth { get; set; }
  37.         public int TileHight { get; set; }
  38.         public int TotalWidth { get; set; }
  39.         public int TotalHeight { get; set; }
  40.         public int AllTiles { get; set; }
  41.         public int TilesPerPercent { get; set; }
  42.         #endregion
  43.  
  44.         #region Controls init.
  45.         public System.Windows.Controls.TextBox LogsTB { get; set; }
  46.         #endregion
  47.  
  48.         public MainWindow()
  49.         {
  50.             LogsTB = LOGS; //init
  51.  
  52.             InitializeComponent();
  53.         }
  54.  
  55.         private void START_Click(object sender, RoutedEventArgs e)
  56.         {
  57.             //ASSIGN VALUE FOR PROP HERE
  58.             try
  59.             {
  60.                 DirectoryInfo mainDirectory = new DirectoryInfo(MainPath);
  61.                 DirectoryInfo[] listOfSubdirectories = mainDirectory.GetDirectories();
  62.  
  63.                 DirectoriesCount = listOfSubdirectories.Length; //Includes count of directories inside main path
  64.                 FileInfo[] tilesWH = listOfSubdirectories[0].GetFiles();// Gets first image to get properties like width and height
  65.                 System.Drawing.Image img = System.Drawing.Image.FromFile(tilesWH[0].FullName);
  66.                 TileWidth = img.Width; //Tile's width
  67.                 TileHight = img.Height; //Tile's height
  68.                 TilesCount = tilesWH.Length; //Tiles per directory
  69.                 TotalWidth = TileWidth * DirectoriesCount; //Total image's width
  70.                 TotalHeight = TileHight * TilesCount; //Total image's height
  71.  
  72.                 if (bgw == null)
  73.                 {
  74.                     bgw = new BackgroundWorker();
  75.                     bgw.DoWork += new DoWorkEventHandler(bgw_DoWork1);
  76.                     bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkedCompleted1);
  77.                     bgw.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged1);
  78.  
  79.                     bgw.WorkerReportsProgress = true;
  80.                     bgw.WorkerSupportsCancellation = true;
  81.                 }
  82.                 MainPROGRESSBAR.Value = 0;
  83.                 LOGS.Text = ("Uruchomiono proces\n");
  84.                 bgw.RunWorkerAsync();
  85.             }
  86.             catch (Exception exc)
  87.             {
  88.                 AppendLog("BŁĄD KURWA " + exc);
  89.             }
  90.         }
  91.  
  92.         private void STOP_Click(object sender, RoutedEventArgs e)
  93.         {
  94.             if (bgw != null && bgw.IsBusy) bgw.CancelAsync();
  95.         }
  96.  
  97.         private void PathSearchBtn_Click(object sender, RoutedEventArgs e)
  98.         {
  99.             using (var fbd = new FolderBrowserDialog())
  100.             {
  101.                 DialogResult result = fbd.ShowDialog();
  102.  
  103.                 if (!string.IsNullOrWhiteSpace(fbd.SelectedPath))
  104.                 {
  105.                     string[] directories = Directory.GetDirectories(fbd.SelectedPath);
  106.                     //System.Windows.Forms.MessageBox.Show("Znalezione foldery: " + directories.Length.ToString(), "Message");
  107.                     //TODO: Sprawdz poprawnosc, czy instnieje dany folder lub wywal błąd
  108.                     MainPath = fbd.SelectedPath;
  109.                     PATH.Text = fbd.SelectedPath;
  110.                 }
  111.             }
  112.         }
  113.  
  114.         private void FilePathSearchBtn_Click(object sender, RoutedEventArgs e)
  115.         {
  116.             Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  117.             dlg.FileName = "map"; // Default file name
  118.             dlg.DefaultExt = ".jpg"; // Default file extension
  119.             dlg.Filter = "Pliki graficzne (*.jpg *.jpeg)|*.jpg; *.jpeg"; // Filter files by extension
  120.  
  121.             // Show save file dialog box
  122.             Nullable<bool> result = dlg.ShowDialog();
  123.  
  124.             // Process save file dialog box results
  125.             if (result == true)
  126.             {
  127.                 // Save document
  128.                 OutputFilePath = dlg.FileName;
  129.                 OUTPUTFILEPATHTB.Text = OutputFilePath;
  130.                 //OUTPUTFILEPATHTB.ScrollToEnd(); //NIE DZIAŁA
  131.             }
  132.         }
  133.  
  134.         #region BGWorker1
  135.  
  136.         private void bgw_DoWork1(object sender, DoWorkEventArgs e)
  137.         {
  138.             DirectoryInfo mainDirectory = new DirectoryInfo(MainPath);
  139.             DirectoryInfo[] listOfSubdirectories = mainDirectory.GetDirectories();
  140.             List<Tiles> tilesList = new List<Tiles>();
  141.             int dir = 0;
  142.             int currentHeight = 0;
  143.             foreach (DirectoryInfo directory in listOfSubdirectories)
  144.             {
  145.                 if (bgw.CancellationPending)
  146.                 {
  147.                     e.Cancel = true;
  148.                     break;
  149.                 }
  150.                 int iteration = 0;
  151.                 FileInfo[] filesInDir = directory.GetFiles();
  152.                 foreach (FileInfo file in filesInDir)
  153.                 {
  154.                     if (bgw.CancellationPending)
  155.                     {
  156.                         e.Cancel = true;
  157.                         break;
  158.                     }
  159.                     if (iteration == 0) //first element
  160.                     {
  161.                         Tiles tile = new Tiles(file.FullName, TileWidth * dir, 0, directory.Name, file.Name);
  162.                         tilesList.Add(tile);
  163.                         iteration++;
  164.                         currentHeight = TileHight;
  165.                     }
  166.                     else
  167.                     {
  168.                         Tiles tile = new Tiles(file.FullName, TileWidth * dir, currentHeight, directory.Name, file.Name);
  169.                         tilesList.Add(tile);
  170.                         iteration++;
  171.                         currentHeight += TileHight;
  172.                     }
  173.                     string action = "Analizowanie " + directory.Name + "/" + file.Name;
  174.                     bgw.ReportProgress(0, action);//END TEMP
  175.                 }
  176.                 dir++;
  177.                 bgw.ReportProgress(dir, "!");//END TEMP
  178.  
  179.             }
  180.             Bitmap merged = new Bitmap(TotalWidth, TotalHeight);
  181.             Graphics g = Graphics.FromImage(merged);
  182.             bgw.ReportProgress(tilesList.Count, "Rozpoczynanie łączenia plików");
  183.             int step = 0;
  184.             AllTiles = tilesList.Count;
  185.             TilesPerPercent = AllTiles / 100;
  186.             foreach (Tiles tile in tilesList)
  187.             {
  188.                 if (bgw.CancellationPending)
  189.                 {
  190.                     e.Cancel = true;
  191.                     break;
  192.                 }
  193.                 g.DrawImage(tile.PartImage, new System.Drawing.Point(tile.X, tile.Y));
  194.                 tile.PartImage.Dispose();
  195.                 bgw.ReportProgress(++step, "Dołączono pliki z folderu: " + tile.DirectoryName);
  196.             }
  197.             bgw.ReportProgress(0, "Trwa zapisywanie pliku, proszę czekać...");
  198.             g.Dispose();
  199.             merged.Save(OutputFilePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  200.             merged.Dispose();
  201.             bgw.ReportProgress(0, "Plik został prawidłowo zapisany");
  202.         }
  203.         private void bgw_ProgressChanged1(object sender, ProgressChangedEventArgs e)
  204.         {
  205.             if (e.UserState.ToString() == "Rozpoczynanie łączenia plików")
  206.                 MainPROGRESSBAR.Maximum = AllTiles;//e.ProgressPercentage;
  207.             else if (e.UserState.ToString()[0] == 'A')
  208.             {
  209.                 AppendLog(e.UserState.ToString());
  210.                 SUBPB.Content = e.UserState.ToString();
  211.             }
  212.             else if (e.UserState.ToString()[0] == 'D')
  213.             {
  214.                 if (e.ProgressPercentage % TilesCount == 0)
  215.                 {
  216.                     SUBPB.Content = e.UserState.ToString();
  217.                     AppendLog(e.UserState.ToString());
  218.                     MainPROGRESSBAR.Value = e.ProgressPercentage;
  219.                 }
  220.             }
  221.             else if (e.UserState.ToString()[0] == '!')
  222.             {
  223.                 MainPROGRESSBAR.Maximum = DirectoriesCount;//e.ProgressPercentage;
  224.                 //AppendLog(e.UserState.ToString());
  225.                 //SUBPB.Content = e.UserState.ToString();
  226.                 MainPROGRESSBAR.Value = e.ProgressPercentage;
  227.             }
  228.             else
  229.             {
  230.                 //MainPROGRESSBAR. zmien kolor jezeli przerwano - nie tutaj
  231.                 AppendLog(e.UserState.ToString());
  232.             }
  233.         }
  234.  
  235.         private void bgw_RunWorkedCompleted1(object sender, RunWorkerCompletedEventArgs e)
  236.         {
  237.             if (e.Cancelled) AppendLog("Operacja została przerwana przez użytkonwika");
  238.             else
  239.             {
  240.                 AppendLog("Program zakończył pracę");
  241.                 MAINPB.Content = "ZAKOŃCZNO";
  242.             }
  243.         }
  244.         #endregion
  245.  
  246.         #region Methods
  247.  
  248.         #endregion
  249.  
  250.  
  251.         #region Helper methods
  252.         private void AppendLog(string s)
  253.         {
  254.             LOGS.AppendText(s + "\n");
  255.             LOGS.ScrollToEnd();
  256.         }
  257.         #endregion
  258.  
  259.  
  260.     }
  261. }
  262.  
  263.  
  264. using System;
  265. using System.Collections.Generic;
  266. using System.Drawing;
  267. using System.Linq;
  268. using System.Text;
  269. using System.Threading.Tasks;
  270.  
  271. namespace MergeR_BGWorker
  272. {
  273.     public class Tiles
  274.     {
  275.         public string Path { get; set; }
  276.         public string TileName { get; set; }
  277.         public string DirectoryName { get; set; }
  278.         public int X { get; set; }
  279.         public int Y { get; set; }
  280.         public Image PartImage { get; set; }
  281.  
  282.         public Tiles(string _path, int x, int y, string dirName, string tileName)
  283.         {
  284.             Path = _path;
  285.             PartImage = Image.FromFile(Path);
  286.             X = x;
  287.             Y = y;
  288.             DirectoryName = dirName;
  289.             TileName = tileName;
  290.         }
  291.     }
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement