private void drawGridForCell(Image img, ImageCell cell, int rowCount, int colCount)
        {
            int cellWidth  = cell.RightLowerCorner.X - cell.LeftUpperCorner.X;
            int cellHeight = cell.RightLowerCorner.Y - cell.LeftUpperCorner.Y;
            int colWidth   = cellWidth / colCount;
            int rowHeight  = cellHeight / rowCount;

            using (Graphics g = Graphics.FromImage(img))
            {
                for (var i = 1; i < colCount; i++)
                {
                    g.DrawLine(
                        new Pen(Color.Black, 1f),
                        new Point(cell.LeftUpperCorner.X + i * colWidth, cell.LeftUpperCorner.Y),
                        new Point(cell.LeftUpperCorner.X + i * colWidth, cell.RightLowerCorner.Y));
                }

                for (var j = 1; j < rowCount; j++)
                {
                    g.DrawLine(
                        new Pen(Color.Black, 1f),
                        new Point(cell.LeftUpperCorner.X, j * rowHeight + cell.LeftUpperCorner.Y),
                        new Point(cell.RightLowerCorner.X, j * rowHeight + cell.LeftUpperCorner.Y));
                }
                var columCenter = cellWidth / 2;
                var rowCenter   = cellHeight / 2;
                var values      = cell.GridValues;
                for (var i = 0; i < colCount; i++)
                {
                    for (var j = 0; j < rowCount; j++)
                    {
                        g.DrawString(values[i, j].ToString(),
                                     new Font("Arial", int.Parse(comboBox1.SelectedItem.ToString())), new SolidBrush(colorDialog.Color),
                                     new Point(i * colWidth + cell.LeftUpperCorner.X,
                                               j * rowHeight + cell.LeftUpperCorner.Y));
                    }
                }
            }
        }
        private void onCalculateButtonClick(object sender, EventArgs e)
        {
            clearHistograms();
            this.chartData       = new Dictionary <string, List <Point> [, ]>();
            sourceImageBox.Image = Image.FromFile(sourceImagePath);
            int    gridColCount        = int.Parse(gridColCountTextBox.Text);
            int    gridRowCount        = int.Parse(gridRowCountTextBox.Text);
            int    cellsColCount       = int.Parse(imagesColCountTextBox.Text);
            int    cellsRowCount       = int.Parse(imagesRowCountTextBox.Text);
            int    maxDifference       = 256 * cellsColCount * cellsRowCount * 2;
            Bitmap preparedSourceImage = MakeGrayAndCut(sourceImageBox.Image);

            pixelsList.Clear();
            for (int i = 0; i < preparedSourceImage.Size.Width; i++)
            {
                int pixelCurr = 0;
                for (int j = 0; j < preparedSourceImage.Size.Height; j++)
                {
                    pixelCurr += (preparedSourceImage.GetPixel(i, j).R +
                                  preparedSourceImage.GetPixel(i, j).G +
                                  preparedSourceImage.GetPixel(i, j).B) / 3;
                }
                pixelsList.Add(pixelCurr);
            }
            if (hisEqCheckbox.Checked)
            {
                preparedSourceImage = ImageUtils.histogramEqualization(preparedSourceImage);
            }
            addHistogram(preparedSourceImage, "Source image");
            List <ImageCell> sourceImageCells = getImageCells(preparedSourceImage, cellsColCount, cellsRowCount, gridColCount, gridRowCount);

            sourceImageBox.Image = (Image)preparedSourceImage;
            addToChartData(sourceImagePath, sourceImageCells, cellsColCount, cellsRowCount);
            sourceImageData = getChartDataEntry(sourceImageCells, cellsColCount, cellsRowCount);
            imageListControl.Items.Clear();
            imlLargeIcons.Images.Clear();
            imlSmallIcons.Images.Clear();
            for (var i = 0; i < imageListPaths.Count; i++)
            {
                var bitmap = MakeGrayAndCut(Image.FromFile(imageListPaths[i]));
                if (hisEqCheckbox.Checked)
                {
                    bitmap = ImageUtils.histogramEqualization(bitmap);
                }
                addHistogram(bitmap, "Image" + i);
                List <ImageCell> cells = getImageCells(bitmap, cellsColCount, cellsRowCount, gridColCount, gridRowCount);
                int difference         = ImageCell.calculateDifference(sourceImageCells, cells);
                imlLargeIcons.AddImage(bitmap, imageListPaths[i]);
                imlSmallIcons.AddImage(bitmap, imageListPaths[i]);
                double percentage = (1.0 - (double)difference / (double)maxDifference) * 100.0;
                string caption    = difference.ToString() + " (" + ((int)percentage).ToString() + "%)";
                imageListControl.AddRow(imageListPaths[i], caption, _filesName[i]);
                if (!this.chartData.ContainsKey(imageListPaths[i]))
                {
                    addToChartData(imageListPaths[i], cells, cellsColCount, cellsRowCount);
                }
            }
            imageListControl.ListViewItemSorter = new ListViewItemComparer();
            CurrentResults = new List <string>();
            foreach (ListViewItem item in imageListControl.Items)
            {
                CurrentResults.Add(item.ImageKey);
            }
            if (sampleSaveChackbox.Checked)
            {
                Comparator = new ResultsComparator();
                Comparator.SampleResults   = CurrentResults;
                sampleSaveChackbox.Checked = false;
            }
            drawCells(sourceImageBox.Image, cellsRowCount, cellsColCount);
            foreach (ImageCell cell in sourceImageCells)
            {
                drawGridForCell(sourceImageBox.Image, cell, gridRowCount, gridColCount);
            }
            Update();
            Refresh();
            chartsButton.Enabled         = true;
            matrixButton.Enabled         = true;
            sourceClassification.Enabled = true;
        }