Example #1
0
        void txt_Click(object sender, EventArgs e, PictureBox img, ComboBox drpd)
        {
            //Task.Run(() =>
            //{
            try
            {
                img.ImageLocation = ((sender as TextBox).Text);
                img.Refresh();

                Image otima = null;
                if (Out.IsLocalFile(img.ImageLocation))
                {
                    otima = Image.FromFile(img.ImageLocation);
                }
                else
                if (Out.URLExists(img.ImageLocation))
                {
                    using (System.Net.WebClient wc = new System.Net.WebClient())
                    {
                        otima = Image.FromStream(new MemoryStream(wc.DownloadData(img.ImageLocation)));
                    }
                }
                if (otima != null)
                {
                    Task.Run(() =>
                    {
                        img.BackColor = PictureAnalysis.GetMostUsedColor(otima).MostUsedColor;
                    });
                }
            }
            catch { }
            //});
        }
Example #2
0
        public static PictureAnalysis GetMostUsedColor(System.Drawing.Image img)
        {
            var theBitMap = new Bitmap(img);

            var ret = new PictureAnalysis();

            ret.TenMostUsedColors          = new List <Color>();
            ret.TenMostUsedColorIncidences = new List <int>();

            ret.MostUsedColor          = Color.Empty;
            ret.MostUsedColorIncidence = 0;

            // does using Dictionary<int,int> here
            // really pay-off compared to using
            // Dictionary<Color, int> ?

            // would using a SortedDictionary be much slower, or ?

            ret.dctColorIncidence = new Dictionary <int, int>();

            // this is what you want to speed up with unmanaged code
            for (int row = 0; row < theBitMap.Size.Width; row++)
            {
                for (int col = 0; col < theBitMap.Size.Height; col++)
                {
                    ret.pixelColor = theBitMap.GetPixel(row, col).ToArgb();

                    if (ret.dctColorIncidence.Keys.Contains(ret.pixelColor))
                    {
                        ret.dctColorIncidence[ret.pixelColor]++;
                    }
                    else
                    {
                        ret.dctColorIncidence.Add(ret.pixelColor, 1);
                    }
                }
            }

            // note that there are those who argue that a
            // .NET Generic Dictionary is never guaranteed
            // to be sorted by methods like this
            var dctSortedByValueHighToLow = ret.dctColorIncidence.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value);

            // this should be replaced with some elegant Linq ?
            foreach (KeyValuePair <int, int> kvp in dctSortedByValueHighToLow.Take(10))
            {
                ret.TenMostUsedColors.Add(Color.FromArgb(kvp.Key));
                ret.TenMostUsedColorIncidences.Add(kvp.Value);
            }

            ret.MostUsedColor          = Color.FromArgb(dctSortedByValueHighToLow.First().Key);
            ret.MostUsedColorIncidence = dctSortedByValueHighToLow.First().Value;

            return(ret);
        }