Ejemplo n.º 1
0
        private void Image_img_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (image_img.Source.ToString() == "pack://application:,,,/Image Surface Comparison System;component/Resources/loadPhoto_img.png")
            {
                //LoadImage();
                return;
            }

            double x = Math.Floor(e.GetPosition(image_img).X *Base.photo.photo.PixelWidth / image_img.ActualWidth);
            double y = Math.Floor(e.GetPosition(image_img).Y *Base.photo.photo.PixelHeight / image_img.ActualHeight);

            int   index    = Base.photo.GetIndex((int)x, (int)y);
            Color selected = null;

            if (Base.selectedTool == 0)
            {
                selected = new Color(Base.photoOriginal.GetColor(index));
                if (autoOtherPhotos_ch.IsChecked == false)
                {
                    WandTool.Wand((int)x, (int)y, selected, (byte)photoDegreeTolerance_s.Value);
                }
                else
                {
                    for (int i = 0; i < photo_cb.Items.Count; i++)
                    {
                        photo_cb.SelectedIndex = i;
                        string filename = Base.path + "\\Photos\\" + album_cb.SelectedValue + "\\" + photo_cb.SelectedValue;
                        Base.lastFilename = album_cb.SelectedValue + "\\" + photo_cb.SelectedValue;

                        BitmapImage b = new BitmapImage();
                        b.BeginInit();
                        b.UriSource = new Uri(filename);
                        b.EndInit();

                        Base.photo         = new Photo((BitmapSource)b);
                        Base.photoOriginal = new Photo((BitmapSource)b);

                        if (Color.Difference(selected, Base.photoOriginal.GetColor(Base.photoOriginal.GetIndex((int)x, (int)y))) < (byte)photoDegreeTolerance_s.Value)
                        {
                            WandTool.Wand((int)x, (int)y, selected, (byte)photoDegreeTolerance_s.Value);
                        }
                        else
                        {
                            int width  = (int)(Base.photoOriginal.width * 0.1);
                            int height = (int)(Base.photoOriginal.height * 0.1);
                            int limit;
                            if (width > height)
                            {
                                limit = width;
                            }
                            else
                            {
                                limit = height;
                            }

                            int xx = (int)x, yy = (int)y;
                            for (int step = 1; step < limit; step++)
                            {
                                int  startX, startY, endX, endY;
                                bool end = false;
                                int  count;
                                if (x - step >= 0)
                                {
                                    startX = (int)x - step;
                                }
                                else
                                {
                                    startX = 0;
                                }

                                if (y - step >= 0)
                                {
                                    startY = (int)y - step;
                                }
                                else
                                {
                                    startY = 0;
                                }

                                if (x + step < Base.photo.width)
                                {
                                    endX = (int)x + step;
                                }
                                else
                                {
                                    endX = Base.photo.width - 1;
                                }

                                if (y + step < Base.photo.height)
                                {
                                    endY = (int)y + step;
                                }
                                else
                                {
                                    endY = Base.photo.height - 1;
                                }

                                count = 0;
                                for (int xxx = startX; xxx < endX; xxx++)
                                {
                                    if (Color.Difference(selected, Base.photoOriginal.GetColor(Base.photoOriginal.GetIndex(xxx, startY))) < (byte)photoDegreeTolerance_s.Value)
                                    {
                                        count++;
                                        if (count > 5)
                                        {
                                            xx  = xxx;
                                            yy  = startY;
                                            end = true;
                                        }
                                    }
                                }
                                count = 0;
                                if (end == false)
                                {
                                    for (int xxx = startX; xxx < endX; xxx++)
                                    {
                                        if (Color.Difference(selected, Base.photoOriginal.GetColor(Base.photoOriginal.GetIndex(xxx, endY))) < (byte)photoDegreeTolerance_s.Value)
                                        {
                                            count++;
                                            if (count > 5)
                                            {
                                                xx  = xxx;
                                                yy  = endY;
                                                end = true;
                                            }
                                        }
                                    }
                                }
                                count = 0;
                                if (end == false)
                                {
                                    for (int yyy = startY + 1; yyy < endY - 1; yyy++)
                                    {
                                        if (Color.Difference(selected, Base.photoOriginal.GetColor(Base.photoOriginal.GetIndex(startX, yyy))) < (byte)photoDegreeTolerance_s.Value)
                                        {
                                            count++;
                                            if (count > 5)
                                            {
                                                xx  = startX;
                                                yy  = yyy;
                                                end = true;
                                            }
                                        }
                                    }
                                }
                                count = 0;
                                if (end == false)
                                {
                                    for (int yyy = startY + 1; yyy < endY - 1; yyy++)
                                    {
                                        if (Color.Difference(selected, Base.photoOriginal.GetColor(Base.photoOriginal.GetIndex(endX, yyy))) < (byte)photoDegreeTolerance_s.Value)
                                        {
                                            count++;
                                            if (count > 5)
                                            {
                                                xx  = endX;
                                                yy  = yyy;
                                                end = true;
                                            }
                                        }
                                    }
                                }
                                if (end == true)
                                {
                                    break;
                                }
                            }
                            WandTool.Wand((int)xx, (int)yy, selected, (byte)photoDegreeTolerance_s.Value);
                        }

                        image_img.Source        = Base.photo.photo;
                        imageOrginal_img.Source = Base.photoOriginal.photo;
                    }
                }

                if (Base.selectedMode == 0)
                {
                    selectionAddMode_rb.IsChecked = true;
                    Base.selectedMode             = 1;
                }
            }
            else if (Base.selectedTool == 1)
            {
                BrushTool.Brush((int)x, (int)y, Int32.Parse(brushSize_tb.Text), brushShape);

                if (Base.selectedMode == 0)
                {
                    selectionAddMode_rb.IsChecked = true;
                    Base.selectedMode             = 1;
                }

                brushDown = true;
            }
            else if (Base.selectedTool == 2 && selectedPolygonState == true)
            {
                AddPoint(e.GetPosition(canvas_c).X, e.GetPosition(canvas_c).Y);
            }
            else if (Base.selectedTool == 3)
            {
                image_img.CaptureMouse();
                originHand = e.GetPosition(image_img);
            }
            else if (Base.selectedTool == 4)
            {
                image_img.RenderTransform = null;
                var position = e.MouseDevice.GetPosition(image_img);

                if (magnifierZoomInMode.IsChecked == true)
                {
                    scale += 0.1;
                }
                else
                {
                    if (scale - 0.1 >= 1)
                    {
                        scale -= 0.1;
                    }
                }

                renderCenterX = position.X;
                renderCenterY = position.Y;

                image_img.RenderTransform        = new ScaleTransform(scale, scale, renderCenterX, renderCenterY);
                imageOrginal_img.RenderTransform = new ScaleTransform(scale, scale, renderCenterX, renderCenterY);
                canvas_c.RenderTransform         = new ScaleTransform(scale, scale, renderCenterX, renderCenterY);
                magnifierZoom_l.Content          = scale * 100 + "%";
            }
            double allPixels = Base.photo.width * Base.photo.height;

            degree_l.Content = string.Format("{0:0.00}", (((double)(Base.photo.selectedPixelsCount) / allPixels)) * 100) + "% \nSelected: " + Base.photo.selectedPixelsCount + "pixels\nAll: " + allPixels + "pixels";
        }
Ejemplo n.º 2
0
        private void Image_img_MouseMove(object sender, MouseEventArgs e)
        {
            if (Base.selectedTool == 1 && brushDown == true)
            {
                double x = Math.Floor(e.GetPosition(image_img).X *Base.photo.photo.PixelWidth / image_img.ActualWidth);
                double y = Math.Floor(e.GetPosition(image_img).Y *Base.photo.photo.PixelHeight / image_img.ActualHeight);

                if (Mouse.LeftButton == MouseButtonState.Pressed)
                {
                    BrushTool.Brush((int)x, (int)y, Int32.Parse(brushSize_tb.Text), brushShape);
                }
                else
                {
                    brushDown = false;
                }
            }
            else if (Base.selectedTool == 2)
            {
                if (selectedPolygonState == true && points.Count > 0)
                {
                    lineTmp.X2 = e.GetPosition(canvas_c).X;
                    lineTmp.Y2 = e.GetPosition(canvas_c).Y + 1;
                }
            }

            else if (Base.selectedTool == 3)
            {
                if (image_img.IsMouseCaptured)
                {
                    deltaHand = e.GetPosition(image_img);

                    renderCenterX = renderCenterX + (originHand.X - deltaHand.X);
                    renderCenterY = renderCenterY + (originHand.Y - deltaHand.Y);

                    image_img.RenderTransform        = new ScaleTransform(scale, scale, renderCenterX, renderCenterY);
                    imageOrginal_img.RenderTransform = new ScaleTransform(scale, scale, renderCenterX, renderCenterY);
                    canvas_c.RenderTransform         = new ScaleTransform(scale, scale, renderCenterX, renderCenterY);
                }
            }

            if (e.MiddleButton == MouseButtonState.Pressed)
            {
                if (displacement == false)
                {
                    originHand   = e.GetPosition(image_img);
                    displacement = true;
                }
                else
                {
                    deltaHand = e.GetPosition(image_img);

                    renderCenterX = renderCenterX + (originHand.X - deltaHand.X);
                    renderCenterY = renderCenterY + (originHand.Y - deltaHand.Y);

                    image_img.RenderTransform        = new ScaleTransform(scale, scale, renderCenterX, renderCenterY);
                    imageOrginal_img.RenderTransform = new ScaleTransform(scale, scale, renderCenterX, renderCenterY);
                    canvas_c.RenderTransform         = new ScaleTransform(scale, scale, renderCenterX, renderCenterY);
                }
            }
            if (e.MiddleButton == MouseButtonState.Released)
            {
                displacement = false;
            }

            if (Base.photo != null)
            {
                double allPixels = Base.photo.width * Base.photo.height;
                degree_l.Content = string.Format("{0:0.00}", (((double)(Base.photo.selectedPixelsCount) / allPixels)) * 100) + "% \nSelected: " + Base.photo.selectedPixelsCount + "pixels\nAll: " + allPixels + "pixels";
            }
        }