Example #1
0
        private async void SearchPhotoButton_ClickAsync(object sender, RoutedEventArgs e)
        {
            try
            {
                GoBackButton.IsEnabled      = false;
                SearchPhotoButton.IsEnabled = false;
                SelectPhotoButton.IsEnabled = false;
                if (FolderTextBox.Text == "" || FullFileName == "")
                {
                    MessageBox.Show("Введите все данные");
                    return;
                }
                DirectoryInfo        directoryInfo        = new DirectoryInfo($@"{FolderTextBox.Text}");
                List <string>        SimilarImagePathList = new List <string>();
                System.Drawing.Image searchImage          = System.Drawing.Image.FromFile(FullFileName);
                ProgressBarWindow    progressBarWindow    = new ProgressBarWindow(directoryInfo.GetFiles().ToList().Count);
                await Task.Run(() =>
                {
                    int counter = 0;
                    Dispatcher.Invoke(() => progressBarWindow.Show());
                    foreach (var file in directoryInfo.GetFiles())
                    {
                        counter++;
                        Dispatcher.Invoke(() => progressBarWindow.ProgressBar.Value = counter);
                        if (System.IO.Path.GetExtension(file.FullName) == ".jpg" || System.IO.Path.GetExtension(file.FullName) == ".png" || System.IO.Path.GetExtension(file.FullName) == ".jpeg")
                        {
                            int countOfComare          = 0;
                            System.Drawing.Image image = System.Drawing.Image.FromFile(file.FullName);
                            byte[,] array = XnaFan.ImageComparison.ExtensionMethods.GetDifferences(searchImage, image);


                            for (int i = 0; i < array.GetLength(0); i++)
                            {
                                for (int j = 0; j < array.GetLength(1); j++)
                                {
                                    if (array[i, j] == 0)
                                    {
                                        countOfComare++;
                                    }
                                }
                            }
                            if (countOfComare > 5)
                            {
                                SimilarImagePathList.Add(file.FullName);
                            }
                        }
                    }
                    Dispatcher.Invoke(() => progressBarWindow.Close());
                    if (SimilarImagePathList.Count > 0)
                    {
                        MessageBox.Show("Совпадения найдены!");
                        Dispatcher.Invoke(() => Helper.GoNextWindow(new User.ShowSimilarPhotoWindow(SimilarImagePathList, false), this));
                        return;
                    }
                    MessageBox.Show("Совпадения в данной папке не найдены");
                });
            }
            catch
            {
                MessageBox.Show("Данный поиск невозможен");
            }
            finally
            {
                GoBackButton.IsEnabled      = true;
                SearchPhotoButton.IsEnabled = true;
                SelectPhotoButton.IsEnabled = true;
            }
        }
        private async void SearchPhotoButton_ClickAsync(object sender, RoutedEventArgs e)
        {
            GoBackButton.IsEnabled      = false;
            SearchPhotoButton.IsEnabled = false;
            SelectPhotoButton.IsEnabled = false;
            if (FullFileName == "")
            {
                MessageBox.Show("Пожалуйста, выберите картинку");
                return;
            }
            try
            {
                int counter = 0;
                PhotoList = Helper.Connection.Photo.ToList();
                System.Drawing.Image searchImage = System.Drawing.Image.FromFile(FullFileName);
                PhotoList = PhotoList.Where(p => p.Category.Title == CategoryComboBox.SelectedItem.ToString()).ToList();
                ProgressBarWindow ProgressBarWindow = new ProgressBarWindow(PhotoList.Count);
                ProgressBarWindow.Show();
                await Task.Run(() =>
                {
                    foreach (Photo p in PhotoList)
                    {
                        counter++;
                        Dispatcher.Invoke(() => ProgressBarWindow.ProgressBar.Value = counter);
                        int counterOfCompare             = 0;
                        System.Drawing.Image imageFromDB = System.Drawing.Image.FromFile(p.Path);
                        byte[,] array = ExtensionMethods.GetDifferences(searchImage, imageFromDB);

                        for (int i = 0; i < array.GetLength(0); i++)
                        {
                            for (int j = 0; j < array.GetLength(1); j++)
                            {
                                if (array[i, j] == 0)
                                {
                                    counterOfCompare++;
                                }
                            }
                        }

                        if (counterOfCompare > 5)
                        {
                            SimilarPhotoList.Add(p);
                        }
                        continue;
                    }
                    Dispatcher.Invoke(() => ProgressBarWindow.Close());
                    if (SimilarPhotoList.Count > 0)
                    {
                        List <string> similarImagePathList = SimilarPhotoList.Select(p => p.Path).ToList();
                        MessageBox.Show("Совпадение найдено!");
                        Dispatcher.Invoke(() => Helper.GoNextWindow(new ShowSimilarPhotoWindow(similarImagePathList, true), this));
                        return;
                    }
                    else
                    {
                        Dispatcher.Invoke(() =>
                        {
                            string categoryTitle = CategoryComboBox.SelectedItem as string;
                            MessageBox.Show($"К сожалению в категории {categoryTitle} нет схожих изображений");
                        });
                    }
                });
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            GoBackButton.IsEnabled      = true;
            SearchPhotoButton.IsEnabled = true;
            SelectPhotoButton.IsEnabled = true;
        }