Ejemplo n.º 1
0
        public Bitmap Mosaic(Image image, List <Image> images, int width, int height, int BaseWidth, int BaseHeight, ProgressBar progressBar)
        {
            //Console.WriteLine("Loading images, please wait");
            //Converting loaded image into bitmap
            Resizer  resizer  = new Resizer();
            Bitmap   imageBit = image.BitmapImage;
            Bitmap   bmp      = (Bitmap)imageBit.Clone();
            Scissors scissors = new Scissors();

            bmp = resizer.ResizeImage(bmp, BaseWidth, BaseHeight);

            int bmpWidth  = bmp.Width / (bmp.Width / width);
            int bmpHeight = bmp.Height / (bmp.Height / height);
            //Console.WriteLine("Dividing Image");
            List <int[]> coords             = new List <int[]>();
            Dictionary <Bitmap, int[]> dict = new Dictionary <Bitmap, int[]>();

            for (int i = 0; i < (bmp.Width / width); i++)
            {
                for (int y = 0; y < (bmp.Height / height); y++)
                {
                    Graphics  gr = Graphics.FromImage(bmp);
                    Rectangle r  = new Rectangle(i * bmpWidth,
                                                 y * bmpHeight,
                                                 bmpWidth,
                                                 bmpHeight);
                    double[] coord    = { r.X, r.Y, r.Width, r.Height };
                    int[]    bitCoord = { (i * bmpWidth),
                                          (y * bmpHeight),
                                          (bmpWidth),
                                          (bmpHeight) };
                    dict.Add(scissors.Crop(bmp, coord), bitCoord);
                    coords.Add(bitCoord);
                }
            }
            List <Bitmap> list = new List <Bitmap>();

            foreach (KeyValuePair <Bitmap, int[]> keys in dict)
            {
                list.Add(keys.Key);
            }

            //Console.WriteLine("Getting Avg RGBS");
            List <int[]> rgbAVG = avgRGB(list);


            List <Bitmap> imagesList = new List <Bitmap>();

            foreach (Image imageInsert in images)
            {
                imagesList.Add(imageInsert.BitmapImage);
            }
            Bitmap           baseImage        = new Bitmap(bmp.Width, bmp.Height);
            int              AvgCont          = 0;
            int              max              = list.Count;
            ColorFilter      CF               = new ColorFilter();
            BackgroundWorker backgroundWorker = new BackgroundWorker();

            progressBar.Value   = 0;
            progressBar.Visible = true;
            progressBar.Maximum = max;
            progressBar.Step    = 1;
            while (true)
            {
                //double porcentage = ((double)AvgCont / (double)max);

                if (AvgCont < max)
                {
                    Color color = Color.FromArgb(rgbAVG[AvgCont][0], rgbAVG[AvgCont][1], rgbAVG[AvgCont][2]);
                    baseImage = InsertImage(baseImage, CF.ApplyFilter(Random(images), color)
                                            , coords[AvgCont][0], coords[AvgCont][1], coords[AvgCont][2], coords[AvgCont][3]);
                    //progressBar.Value=((AvgCont * 100) / max);
                    progressBar.PerformStep();
                }
                else
                {
                    progressBar.Visible = false;
                    progressBar.Value   = 0;
                    return(baseImage);
                }
                AvgCont++;
                GC.Collect();
            }
        }