Exemple #1
0
 void LoadImage(Image image)
 {
     ImageCache.SetImage(image as Bitmap);
     pictureBox.Image = image;
     originImage      = null;
     if (!pictureBox.Visible)
     {
         lbTip.Visible = pictureBox.Visible = true;
         lbTip.BringToFront();
     }
     FixSize();
 }
Exemple #2
0
        /// <summary>
        /// 获取指定方向上与指定颜色不同的线段
        /// </summary>
        /// <param name="point"></param>
        /// <param name="color"></param>
        /// <param name="orient"></param>
        /// <param name="span"></param>
        static void GetLineSpan(Point point, Color color, Orientions orient, LineSpan span)
        {
            span.Reset(point);

            if (orient == Orientions.Horizontal)
            {
                // 向左
                for (int i = point.X; i >= 0; i--)
                {
                    if (IsSimilarColor(color, ImageCache.Get(i, point.Y)))
                    {
                        span.FromX = i;
                        break;
                    }
                }
                // 向右
                for (int i = point.X; i < ImageCache.Width; i++)
                {
                    if (IsSimilarColor(color, ImageCache.Get(i, point.Y)))
                    {
                        span.ToX = i;
                        break;
                    }
                }
                return;
            }

            // 向上
            for (int i = point.Y; i >= 0; i--)
            {
                if (IsSimilarColor(color, ImageCache.Get(point.X, i)))
                {
                    span.FromY = i;
                    break;
                }
            }
            // 向下
            for (int i = point.Y; i < ImageCache.Height; i++)
            {
                if (IsSimilarColor(color, ImageCache.Get(point.X, i)))
                {
                    span.ToY = i;
                    break;
                }
            }
        }
Exemple #3
0
        private void menuPaste_Click(object sender, EventArgs e)
        {
            if (Clipboard.ContainsImage())
            {
                var img = Clipboard.GetImage();
                ImageCache.SetImage(img as Bitmap);
                Text = string.Format("图片查看器 - 来自剪贴板 ({0}x{1})", ImageCache.Width, ImageCache.Height);
                LoadImage(img);
                return;
            }

            if (Clipboard.ContainsFileDropList())
            {
                var file = Clipboard.GetFileDropList().Cast <string>()
                           .Where(f => ViewerUtil.SUPPORTED_IMAGES_TYPES.Contains(Path.GetExtension(f))).FirstOrDefault();
                if (file == null)
                {
                    return;
                }
                LoadImage(file);
            }
        }
Exemple #4
0
        /// <summary>
        /// 获取指定点最近的闭合区域
        /// </summary>
        /// <param name="img"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="drawLine"></param>
        /// <param name="drawBorder"></param>
        /// <returns></returns>
        internal static Rectangle GetNearestArea(Bitmap img, int x, int y, bool drawLine, bool drawBorder)
        {
            var width  = ImageCache.Width;
            var height = ImageCache.Height;

            // step1: 取到当前点的颜色
            var color = ImageCache.Get(x, y);

            fillColor = GetContrastColor(color);
            // step1: 找到最近的不同颜色
            // 从4个方向,按顺序 上右下左 依次判断

            // 取色的偏移量
            var offset = 1;
            // 找到不同点后,用来填充的颜色
            //var fill = Color.Red;

            // 找出四个方向的不同点
            Color topColor        = Color.Empty,
                  rightColor      = Color.Empty,
                  bottomColor     = Color.Empty,
                  leftColor       = Color.Empty,
                  lastTopColor    = Color.Empty,
                  lastRightColor  = Color.Empty,
                  lastBottomColor = Color.Empty,
                  lastLeftColor   = Color.Empty;
            Point topPos          = Point.Empty,
                  rightPos        = Point.Empty,
                  bottomPos       = Point.Empty,
                  leftPos         = Point.Empty;

            //var topSpan = new LineSpan();
            //var rightSpan = new LineSpan();
            //var bottomSpan = new LineSpan();
            //var leftSpan = new LineSpan();

            while (true)
            {
                // 上
                var top = y - offset;
                if (topPos.IsEmpty)
                {
                    if (top < 0)
                    {
                        // 边界处理
                        topPos   = new Point(x, 0);
                        topColor = ImageCache.Get(x, 0);
                        //drawLine(img, x, 0, Orient.Horison);
                    }
                    else
                    {
                        topColor = ImageCache.Get(x, top);
                        if (IsSimilarColor(topColor, color) || IsSimilarColor(topColor, lastTopColor))
                        {
                            lastTopColor = topColor;
                            topColor     = Color.Empty;
                        }
                        else
                        {
                            topPos = new Point(x, top);
                            //img.SetPixel(x, top, fill);
                            //drawLine(img, x, top, Orient.Horison);
                        }
                    }
                }
                // 右
                var right = x + offset;
                if (rightPos.IsEmpty)
                {
                    if (right >= width)
                    {
                        // 边界处理
                        rightPos   = new Point(width - 1, y);
                        rightColor = ImageCache.Get(width - 1, y);
                        //drawLine(img, width - 1, y, Orient.Vertical);
                    }
                    else
                    {
                        rightColor = ImageCache.Get(right, y);

                        if (IsSimilarColor(rightColor, color) || IsSimilarColor(rightColor, lastRightColor))
                        {
                            lastRightColor = rightColor;
                            rightColor     = Color.Empty;
                        }
                        else
                        {
                            rightPos = new Point(right, y);
                            //img.SetPixel(right, y, fill);
                            //drawLine(img, right, y, Orient.Vertical);
                        }
                    }
                }
                // 下
                var bottom = y + offset;
                if (bottomPos.IsEmpty)
                {
                    if (bottom >= height)
                    {
                        // 边界处理
                        bottomPos   = new Point(x, height - 1);
                        bottomColor = ImageCache.Get(x, height - 1);
                        //drawLine(img, x, height - 1, Orient.Horison);
                    }
                    else
                    {
                        bottomColor = ImageCache.Get(x, bottom);
                        if (IsSimilarColor(bottomColor, color) || IsSimilarColor(bottomColor, lastBottomColor))
                        {
                            lastBottomColor = bottomColor;
                            bottomColor     = Color.Empty;
                        }
                        else
                        {
                            bottomPos = new Point(x, bottom);
                            //img.SetPixel(x, bottom, fill);
                            //drawLine(img, x, bottom, Orient.Horison);
                        }
                    }
                }
                // 左
                var left = x - offset;
                if (leftPos.IsEmpty && left >= 0)
                {
                    if (left < 0)
                    {
                        // 边界处理
                        leftPos   = new Point(0, y);
                        leftColor = ImageCache.Get(0, y);
                        //drawLine(img, 0, y, Orient.Vertical);
                    }
                    else
                    {
                        leftColor = ImageCache.Get(left, y);
                        if (IsSimilarColor(leftColor, color) || IsSimilarColor(leftColor, lastLeftColor))
                        {
                            lastLeftColor = leftColor;
                            leftColor     = Color.Empty;
                        }
                        else
                        {
                            leftPos = new Point(left, y);
                            //img.SetPixel(left, y, fill);
                            //drawLine(img, left, y, Orient.Vertical);
                        }
                    }
                }

                // 4个方向都找到了
                if (!topPos.IsEmpty && !rightPos.IsEmpty && !bottomPos.IsEmpty && !leftPos.IsEmpty)
                {
                    //if (IsPointsValid(img, color,
                    //    ref topPos, ref rightPos, ref bottomPos, ref leftPos,
                    //    topColor, rightColor, bottomColor, leftColor,
                    //    topSpan, rightSpan, bottomSpan, leftSpan))
                    {
                        break;
                    }
                    //continue;
                }

                // 四个方向都找不到
                if (left < 0 && right >= width && top < 0 && bottom >= height)
                {
                    //Console.WriteLine("4个方向都找不到不同的点");
                    break;
                }

                offset++;
            }

            if (drawLine)
            {
                // 从光标处画十字线

                // 画水平线
                for (int i = leftPos.X; i < rightPos.X; i++)
                {
                    img.SetPixel(i, y, fillColor);
                }
                // 画垂直线
                for (int i = topPos.Y; i < bottomPos.Y; i++)
                {
                    img.SetPixel(x, i, fillColor);
                }
            }

            //Console.WriteLine("TOP:{0},RIGHT:{1},BOTTOM:{2},LEFT:{3}", topPos, rightPos, bottomPos, leftPos);

            var rect = new Rectangle(leftPos.X, topPos.Y, rightPos.X - leftPos.X, bottomPos.Y - topPos.Y);

            if (drawBorder)
            {
                var rightTop    = new Point(rect.X + rect.Width, rect.Y);
                var rightBottom = new Point(rect.X + rect.Width, rect.Y + rect.Height);
                var leftBottom  = new Point(rect.X, rect.Y + rect.Height);
                var leftTop     = new Point(rect.X, rect.Y);

                // top
                DrawLine(img, rightTop, rightBottom);
                // right
                DrawLine(img, leftTop, rightTop);
                // bottom
                DrawLine(img, rightBottom, leftBottom);
                // left
                DrawLine(img, leftBottom, leftTop);
            }
            return(rect);
        }