示例#1
0
        private void btnCompare_Click(object sender, EventArgs e)
        {
            _bmp1.Save("bmp1.png");
            _bmp2.Save("bmp2.png");
            var pct = HashCollection.CompareHashes(_bmp1.GetHash(_scaleSize, "bmp1"), _bmp2.GetHash(_scaleSize, "bmp2"));

            lblPercentMatch.Text    = Math.Round(pct, 3) + "%";
            lblPercentMatch.Visible = true;
        }
示例#2
0
        private void ProcessingThread(string[] files)
        {
            void UpdateProgBar(int c, int newmax = -1, int secondarycount = -1)
            {
                this.AsyncInvokeIfRequired(() => {
                    if (newmax >= 0)
                    {
                        progbar.Maximum = newmax;
                    }
                    progbar.Value     = c;
                    lblFileCount.Text = c + (secondarycount >= 0 ? ": " + secondarycount : "");
                });
            }

            int counter = 0;

            void UpdateProgBarCounter()
            {
                ++counter;
                if (counter % 10 == 0)
                {
                    this.AsyncInvokeIfRequired(() => {
                        progbar.Value     = counter;
                        lblFileCount.Text = counter.ToString();
                    });
                }
            }

            int started  = 0;
            int finished = 0;

            void LoadSomeFiles(object data)
            {
                ++started;
                var f        = (string[])((object[])data)[0];
                var start    = (int)((object[])data)[1];
                var len      = (int)((object[])data)[2];
                var hashcoll = (HashCollection)((object[])data)[3];

                for (int j = 0; j < len; ++j)
                {
                    if (_closed || _stop)
                    {
                        return;
                    }
                    using (Bitmap bmp = LoadBitmap(f[j + start])) {
                        var hash = bmp.GetHash(_scaleSize);
                        lock (hashcoll) {
                            hashcoll.Add(new HashEntry {
                                Filename = f[j + start], Hash = hash
                            });
                        }
                    }
                    UpdateProgBarCounter();
                    Thread.Sleep(0);
                }
                ++finished;
            }

            var hashes = new HashCollection();

            for (int k = 0; k < files.Length; k += 50)
            {
                new Thread(LoadSomeFiles).Start(new object[] { files, k, Math.Min(50, files.Length - k), hashes });
            }
            while (started == 0)
            {
                if (_closed || _stop)
                {
                    return;
                }
                Thread.Sleep(0);
            }
            while (started != finished)
            {
                if (_closed || _stop)
                {
                    return;
                }
                Thread.Sleep(0);
            }
            //int i = 0;
            //foreach (var file in files) {
            //    if (_closed || _stop) return;
            //    using (Bitmap bmp = LoadBitmap(file)) {
            //        var hash = bmp.GetHash(_scaleSize);
            //        hashes.Add(new HashEntry {Filename = file, Hash = hash});
            //        UpdateProgBar(++i);
            //    }
            //    Thread.Sleep(0);
            //}
            UpdateProgBar(0, hashes.Count);
            var minpct = (double)numMinPercent.Value - 0.0001;

            for (int i = 0; i < hashes.Count; ++i)
            {
                var hash1 = hashes[i];
                for (int j = i + 1; j < hashes.Count; ++j)
                {
                    if (_closed || _stop)
                    {
                        return;
                    }
                    var hash2 = hashes[j];
                    var pct   = HashCollection.CompareHashes(hash1.Hash, hash2.Hash);
                    if (pct >= minpct)
                    {
                        this.AsyncInvokeIfRequired(() => {
                            string s = hash1.FilenameNoPath + " = " + hash2.FilenameNoPath;
                            if (listMatches.Items.Cast <object>().ToArray().Any(o => o.ToString() == s))
                            {
                                return;
                            }
                            listMatches.Items.Add(new ListBoxEntry {
                                Text = s, Hashes = new[] { hash1, hash2 }
                            });
                        });
                    }
                    if (j % 100 == 0)
                    {
                        UpdateProgBar(i, -1, j);
                        Thread.Sleep(10);
                    }
                }
                UpdateProgBar(i + 1);
            }
            this.AsyncInvokeIfRequired(() => {
                progbar.Visible      = false;
                lblFileCount.Visible = false;
            });
        }
        private void SaveArtworkFile(IITArtwork art, string name)
        {
            try {
                string ext;
                switch (art.Format)
                {
                case ITArtworkFormat.ITArtworkFormatJPEG:
                    ext = "jpg";
                    break;

                case ITArtworkFormat.ITArtworkFormatPNG:
                    ext = "png";
                    break;

                case ITArtworkFormat.ITArtworkFormatBMP:
                    ext = "bmp";
                    break;

                default: return;
                }
                name = name.NormalizeFilename();
                string filepath = Path.Combine(_artfolder, name + ".");
                // don't write a jpg if there's already a png, no matter how different they look to CompareImages.
                if (!File.Exists(filepath + "jpg") && !File.Exists(filepath + "png") && !File.Exists(filepath + "bmp"))
                {
                    art.SaveArtworkToFile(filepath + ext);
                    int[] hash;
                    using (Bitmap bmp = new Bitmap(filepath + ext)) {
                        hash = bmp.GetHash(10);
                    }
                    bool found = false;
                    foreach (var h in _hashes)
                    {
                        if (HashCollection.CompareHashes(hash, h.Hash) >= 97)
                        {
                            found = true;
                            File.Delete(filepath + ext);
                        }
                    }
                    if (!found)
                    {
                        _hashes.Add(new HashEntry {
                            Filename = filepath + ext, Hash = hash
                        });
                        _hashes.Write(_hashFile);
                    }
                }
                else
                {
                    string fname = File.Exists(filepath + "jpg") ? filepath + "jpg" : File.Exists(filepath + "png") ? filepath + "png" : filepath + "bmp";
                    int[]  hash;
                    using (Bitmap bmp = new Bitmap(fname)) {
                        hash = bmp.GetHash(10);
                    }
                    if (!_hashes.Any(h => HashCollection.CompareHashes(h.Hash, hash) >= 97))
                    {
                        _hashes.Add(new HashEntry {
                            Filename = fname, Hash = hash
                        });
                        _hashes.Write(_hashFile);
                    }
                }
            }
            catch (Exception ex) {
                if (!ex.Message.Contains("track has been deleted"))
                {
                    MessageBox.Show("Exception in iTunesRatingControl: " + ex.Message);
                }
            }
        }