Beispiel #1
0
        public IList <ImageFile> GetImageFileListFromFolder(string folderPath)
        {
            IList <ImageFile> result = new List <ImageFile>();

            try
            {
                //search all subfolders for files and return new FileInfo object
                var directoryFiles = Directory
                                     .EnumerateFiles(folderPath, "*", SearchOption.AllDirectories)
                                     .Where(f => _imageExts.Any(x => f.ToLower().EndsWith(x.ToLower())))
                                     .Select(x => new FileInfo(x))
                ;

                foreach (var file in directoryFiles)
                {
                    var theImage = _imageManager.CreateBitmapFromFilePath(file.FullName);

                    result.Add(new ImageFile()
                    {
                        Name     = file.Name,
                        FullPath = file.DirectoryName,
                        Image    = theImage
                    });
                }

                return(result);
            }
            catch (Exception e)
            {
                _consolePrinter.PrintError("There was an error reading the folder", e);

                throw;
            }
        }
Beispiel #2
0
        public void Start()
        {
            _consolePrinter.PrintWelcomeMessage();

            var filesNotRead = true;

            //Get input from user
            while (filesNotRead)
            {
                try
                {
                    var folderPath = _consolePrinter.GetEntryFromUser(
                        "Please enter the location of the merged and duplicated photos, or Q  and enter to quit \r\n\nEg c:\\merged photos\\ "
                        );

                    if (folderPath.ToLower() == "q")
                    {
                        Stop();
                    }

                    var folderExists = _fileManager.DoesFolderExist(folderPath);

                    while (!folderExists)
                    {
                        //Get valid input from user

                        folderPath = _consolePrinter.GetEntryFromUser("The folder entered was not found, please check and try again, or press Q and enter to quit"
                                                                      );

                        if (folderPath.ToLower() == "q")
                        {
                            Stop();
                        }

                        folderExists = _fileManager.DoesFolderExist(folderPath);
                    }

                    _consolePrinter.PrintMessage(
                        "Starting read of folder and sub folders, any import errors will be listed below, but not stop the process\r\n"
                        );

                    var files = _fileManager.GetImageFileListFromFolder(folderPath);

                    filesNotRead = false;

                    var fileCount = files.Count;

                    var userNotResponded = true;

                    while (userNotResponded)
                    {
                        var response = _consolePrinter.GetEntryFromUser(
                            "\r\n" +
                            $"{fileCount:##,###}" +
                            " files have been found, high file counts can take a long time to sort for duplicates and will impede your system performance ! \r\nDo you want to continue ? (Y\\N)"
                            );

                        if (response.ToLower() == "y" || response.ToLower() == "n")
                        {
                            userNotResponded = false;

                            if (response.ToLower() == "y")
                            {
                                var duplicates = FindImages(_imageManager, files);

                                DisplayDuplicates(duplicates);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    _consolePrinter.PrintError("", e);
                }
            }

            _consolePrinter.PrintMessage("Good bye!");
        }
Beispiel #3
0
        public int CompareTwoImages(Bitmap inputImageOne, Bitmap inputImageTwo)
        {
            //Adapted from https://stackoverflow.com/a/21790555/3749506

            if (inputImageOne == null || inputImageTwo == null)
            {
                return(ErrorCode);
            }

            try
            {
                Bitmap imageOne;
                Bitmap imageTwo;

                // lock objects for multi threading
                lock (inputImageOne)
                {
                    imageOne = new Bitmap(inputImageOne, new Size(128, 128));
                }

                lock (inputImageTwo)
                {
                    imageTwo = new Bitmap(inputImageTwo, new Size(128, 128));
                }

                //Create / merge images to the same height/width

                int imageOneSize = imageOne.Width * imageOne.Height;
                int imageTwoSize = imageTwo.Width * imageTwo.Height;

                Bitmap imageThree;

                if (imageOneSize > imageTwoSize)
                {
                    imageOne   = new Bitmap(imageOne, imageTwo.Size);
                    imageThree = new Bitmap(imageTwo.Width, imageTwo.Height);
                }
                else
                {
                    imageOne   = new Bitmap(imageOne, imageTwo.Size);
                    imageThree = new Bitmap(imageTwo.Width, imageTwo.Height);
                }

                //compare pixels for similarity

                for (int x = 0; x < imageOne.Width; x++)
                {
                    for (int y = 0; y < imageOne.Height; y++)
                    {
                        Color colorOne = imageOne.GetPixel(x, y);
                        Color colorTwo = imageTwo.GetPixel(x, y);
                        int   r        = colorOne.R > colorTwo.R ? colorOne.R - colorTwo.R : colorTwo.R - colorOne.R;
                        int   g        = colorOne.G > colorTwo.G ? colorOne.G - colorTwo.G : colorTwo.G - colorOne.G;
                        int   b        = colorOne.B > colorTwo.B ? colorOne.B - colorTwo.B : colorTwo.B - colorOne.B;
                        imageThree.SetPixel(x, y, Color.FromArgb(r, g, b));
                    }
                }

                var difference = 0;

                for (var x = 0; x < imageOne.Width; x++)
                {
                    for (var y = 0; y < imageOne.Height; y++)
                    {
                        var colorOne = imageThree.GetPixel(x, y);
                        var media    = (colorOne.R + colorOne.G + colorOne.B) / 3;
                        if (media > Tolerance)
                        {
                            difference++;
                        }
                    }
                }

                //return likely hood they're the same

                var usedSize = imageOneSize > imageTwoSize ? imageTwoSize : imageOneSize;
                var result   = difference * 100 / usedSize;

                return(result);
            }
            catch (Exception e)
            {
                _consolePrinter.PrintError("Error comparing image files", e);
                throw;
            }
        }