private void add_character_button_Click(object sender, RoutedEventArgs e)
        {
            int res = checkIfAllCharactersAdded();

            if (res == -1)
            {
                System.Windows.MessageBox.Show("You have added all characters");
            }
            else if (isImageOpened)
            {
                Bitmap    buffBitmap = (Bitmap)Image.FromFile(cachedPreviewName);
                Character newC       = new Character(NeededChars[res]);

                System.Drawing.Point screenStart = convertToPixelsD(new System.Windows.Point(
                                                                        Scroller.HorizontalOffset, Scroller.VerticalOffset));
                System.Drawing.Point startPoint  = screenStart;
                System.Drawing.Point finishPoint = new System.Drawing.Point(screenStart.X + 100, screenStart.Y + 100);
                System.Drawing.Point beginPoint  = new System.Drawing.Point(25, 25);
                System.Drawing.Point endPoint    = new System.Drawing.Point(50, 50);
                newC.AddSample(new CharacterSample(BitmapFuncs.cropBitmap(buffBitmap, startPoint, finishPoint),
                                                   beginPoint, endPoint));
                newC.Samples[0].bitmapLoc = startPoint;
                Characters.Add(newC);
                updateLists();
                buffBitmap.Dispose();
            }
            else
            {
                System.Windows.MessageBox.Show("Firstly open image.", "Error",
                                               MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        private void add_variant_button_Click(object sender, RoutedEventArgs e)
        {
            Bitmap buffBitmap = (Bitmap)Image.FromFile(cachedPreviewName);

            System.Drawing.Point screenStart = convertToPixelsD(new System.Windows.Point(
                                                                    Scroller.HorizontalOffset, Scroller.VerticalOffset));
            System.Drawing.Point startPoint  = screenStart;
            System.Drawing.Point finishPoint = new System.Drawing.Point(screenStart.X + 100, screenStart.Y + 100);
            System.Drawing.Point beginPoint  = new System.Drawing.Point(25, 25);
            System.Drawing.Point endPoint    = new System.Drawing.Point(50, 50);
            Characters[selectedCharIndex].AddSample(new CharacterSample(BitmapFuncs.cropBitmap(buffBitmap, startPoint, finishPoint),
                                                                        beginPoint, endPoint));
            Characters[selectedCharIndex].Samples.Last().bitmapLoc = startPoint;
            updateLists();
            buffBitmap.Dispose();
        }
        void showControls(CharacterSample sample)
        {
            System.Drawing.Point startPoint = new System.Drawing.Point(sample.bitmapLoc.X,
                                                                       sample.bitmapLoc.Y + sample.Img.Height);
            System.Drawing.Point finishPoint = new System.Drawing.Point(sample.bitmapLoc.X +
                                                                        sample.Img.Width, sample.bitmapLoc.Y);
            System.Drawing.Point beginPoint = new System.Drawing.Point(
                sample.Begin.X + sample.bitmapLoc.X, sample.Begin.Y + sample.bitmapLoc.Y);
            System.Drawing.Point endPoint = new System.Drawing.Point(
                sample.End.X + sample.bitmapLoc.X, sample.End.Y + sample.bitmapLoc.Y);

            ControlsCanvas.Children.Clear();

            /*bitmap = (Bitmap)buffBitmap.Clone();
             * graphics = Graphics.FromImage(bitmap);*/
            drawPointControl(startPoint, Colors.Black);
            drawPointControl(finishPoint, Colors.Black);
            drawPointControl(beginPoint, Colors.Green);
            drawPointControl(endPoint, Colors.Red);
            SolidColorBrush brush      = new SolidColorBrush(Colors.Black);
            Line            firstLine  = createLine(startPoint.X, finishPoint.Y, finishPoint.X, finishPoint.Y);
            Line            secondLine = createLine(finishPoint.X, finishPoint.Y, finishPoint.X, startPoint.Y);
            Line            thirdLine  = createLine(finishPoint.X, startPoint.Y, startPoint.X, startPoint.Y);
            Line            fourthLine = createLine(startPoint.X, startPoint.Y, startPoint.X, finishPoint.Y);

            firstLine.Stroke = secondLine.Stroke = thirdLine.Stroke = fourthLine.Stroke = brush;
            ControlsCanvas.Children.Add(firstLine);
            ControlsCanvas.Children.Add(secondLine);
            ControlsCanvas.Children.Add(thirdLine);
            ControlsCanvas.Children.Add(fourthLine);

            /*System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black,
             *  1f / (float)PreviewScale.ScaleX);
             * graphics.DrawLine(pen, startPoint.X, finishPoint.Y, finishPoint.X, finishPoint.Y);
             * graphics.DrawLine(pen, finishPoint.X, finishPoint.Y, finishPoint.X, startPoint.Y);
             * graphics.DrawLine(pen, finishPoint.X, startPoint.Y, startPoint.X, startPoint.Y);
             * graphics.DrawLine(pen, startPoint.X, startPoint.Y, startPoint.X, finishPoint.Y);
             * onBitmapChanged();*/

            SymbolPreview.Source = BitmapFuncs.fromBitmap(sample.Img);
        }
 void onBitmapChanged(Bitmap newBitmap)
 {
     Preview.Source = BitmapFuncs.fromBitmap(newBitmap);
 }
        private void Preview_MouseDown(object sender, MouseButtonEventArgs e)
        {
            System.Windows.Point pos = convertToPixels(e.GetPosition(Preview));
            int             x        = (int)pos.X;
            int             y        = (int)pos.Y;
            CharacterSample sample   = Characters[selectedCharIndex].Samples[selectedSampleIndex];

            System.Drawing.Point point      = new System.Drawing.Point(x, y);
            System.Drawing.Point startPoint = new System.Drawing.Point(sample.bitmapLoc.X,
                                                                       sample.bitmapLoc.Y + sample.Img.Height);
            System.Drawing.Point finishPoint = new System.Drawing.Point(sample.bitmapLoc.X +
                                                                        sample.Img.Width, sample.bitmapLoc.Y);
            System.Drawing.Point beginPoint = new System.Drawing.Point(
                sample.Begin.X + sample.bitmapLoc.X, sample.Begin.Y + sample.bitmapLoc.Y);
            System.Drawing.Point endPoint = new System.Drawing.Point(
                sample.End.X + sample.bitmapLoc.X, sample.End.Y + sample.bitmapLoc.Y);

            if (action == NowAction.WaitingForAction)
            {
                if (checkIfInCircle(startPoint, point))
                {
                    action = NowAction.SettingStartPoint;
                }
                else if (checkIfInCircle(finishPoint, point))
                {
                    action = NowAction.SettingFinishPoint;
                }
                else if (checkIfInCircle(beginPoint, point))
                {
                    action = NowAction.SettingBeginPoint;
                }
                else if (checkIfInCircle(endPoint, point))
                {
                    action = NowAction.SettingEndPoint;
                }
            }
            else if (action == NowAction.SettingStartPoint)
            {
                Bitmap buffBitmap = (Bitmap)Image.FromFile(cachedPreviewName);
                int    X          = Math.Min(x, finishPoint.X);
                int    Y          = Math.Min(y, finishPoint.Y);
                int    W          = Math.Abs(finishPoint.X - x);
                int    H          = Math.Abs(y - finishPoint.Y);
                Characters[selectedCharIndex].Samples[selectedSampleIndex].Img =
                    BitmapFuncs.cropBitmap(buffBitmap, new System.Drawing.Rectangle(X, Y, W, H));
                Characters[selectedCharIndex].Samples[selectedSampleIndex].bitmapLoc =
                    new System.Drawing.Point(X, Y);
                showControls(Characters[selectedCharIndex].Samples[selectedSampleIndex]);
                action = NowAction.WaitingForAction;
            }
            else if (action == NowAction.SettingFinishPoint)
            {
                Bitmap buffBitmap = (Bitmap)Image.FromFile(cachedPreviewName);
                int    X          = Math.Min(x, startPoint.X);
                int    Y          = Math.Min(y, startPoint.Y);
                int    W          = Math.Abs(startPoint.X - x);
                int    H          = Math.Abs(y - startPoint.Y);
                Characters[selectedCharIndex].Samples[selectedSampleIndex].Img =
                    BitmapFuncs.cropBitmap(buffBitmap, new System.Drawing.Rectangle(X, Y, W, H));
                Characters[selectedCharIndex].Samples[selectedSampleIndex].bitmapLoc =
                    new System.Drawing.Point(X, Y);
                showControls(Characters[selectedCharIndex].Samples[selectedSampleIndex]);
                action = NowAction.WaitingForAction;
            }
            else if (action == NowAction.SettingEndPoint)
            {
                endPoint = new System.Drawing.Point(
                    x - sample.bitmapLoc.X, y - sample.bitmapLoc.Y);
                Characters[selectedCharIndex].Samples[selectedSampleIndex].End =
                    endPoint;
                showControls(Characters[selectedCharIndex].Samples[selectedSampleIndex]);
                action = NowAction.WaitingForAction;
            }
            else if (action == NowAction.SettingBeginPoint)
            {
                beginPoint = new System.Drawing.Point(
                    x - sample.bitmapLoc.X, y - sample.bitmapLoc.Y);
                Characters[selectedCharIndex].Samples[selectedSampleIndex].Begin =
                    beginPoint;
                showControls(Characters[selectedCharIndex].Samples[selectedSampleIndex]);
                action = NowAction.WaitingForAction;
            }
            else if (action == NowAction.ClearingBack)
            {
                Bitmap bitmap = (Bitmap)Image.FromFile(cachedPreviewName);
                System.Drawing.Color pixel = bitmap.GetPixel(x, y);
                Characters[selectedCharIndex].Samples[selectedSampleIndex].Img =
                    BitmapFuncs.replaceColor(Characters[selectedCharIndex].Samples[selectedSampleIndex].Img,
                                             pixel, System.Drawing.Color.Transparent, FillStrenght.Value);
                showControls(Characters[selectedCharIndex].Samples[selectedSampleIndex]);
                action = NowAction.WaitingForAction;
            }
        }