Exemplo n.º 1
0
        private System.Drawing.Region BuildRegionFromBitmap(BitmapSource bitmap, Point offset)
        {
            var region = new System.Drawing.Region();

            region.MakeEmpty();

            var  rectangle = System.Drawing.Rectangle.Empty;
            bool inImage   = false;

            for (int y = 0; y < bitmap.Height; y++)
            {
                for (int x = 0; x < bitmap.Width; x++)
                {
                    var    croppedPixel = new CroppedBitmap(bitmap, new Int32Rect(x, y, 1, 1));
                    byte[] pixel        = new byte[bitmap.Format.BitsPerPixel / 8];
                    croppedPixel.CopyPixels(pixel, bitmap.Format.BitsPerPixel / 8, 0);
                    var pixelAlpha = pixel[0];

                    if (!inImage && pixelAlpha > 0)
                    {
                        inImage          = true;
                        rectangle.X      = x;
                        rectangle.Y      = y;
                        rectangle.Height = 1;
                    }
                    else if (inImage && pixelAlpha == 0)
                    {
                        inImage         = false;
                        rectangle.Width = x - rectangle.X;
                        rectangle.Offset((int)offset.X, (int)offset.Y);
                        region.Union(rectangle);
                    }
                }

                if (inImage)
                {
                    inImage         = false;
                    rectangle.Width = (int)bitmap.Width - rectangle.X;
                    rectangle.Offset((int)offset.X, (int)offset.Y);
                    region.Union(rectangle);
                }
            }

            return(region);
        }
Exemplo n.º 2
0
        public void Clip(IReadOnlyRegion2D region)
        {
            var clip = new System.Drawing.Region(System.Drawing.Rectangle.Empty);

            foreach (var rect in region.Rects)
            {
                clip.Union(new System.Drawing.Rectangle(rect.X, rect.Y, rect.Width, rect.Height));
            }
            Graphics.SetClip(clip, System.Drawing.Drawing2D.CombineMode.Replace);
        }
Exemplo n.º 3
0
 public void add(Area rhs)
 {
     if (rhs == null)
     {
         return;
     }
     if (rhs.region == null)
     {
         return;
     }
     if (region == null)
     {
         region = new System.Drawing.Region();
     }
     region.Union(rhs.region);
 }
        /// <summary>
        /// 点在区域中是否可见(现已废弃,替代方法 PointInPolygon)
        /// </summary>
        /// <param name="point"></param>
        /// <param name="pointColl"></param>
        /// <returns></returns>
        public static bool IsVisible_Region(Point point, List <Point> pointColl)
        {
            System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
            System.Drawing.Region       region         = new System.Drawing.Region();
            List <System.Drawing.Point> pathPoints     = new List <System.Drawing.Point>();

            foreach (var pathPoint in pointColl)
            {
                pathPoints.Add(new System.Drawing.Point((int)Math.Round(pathPoint.X), (int)Math.Round(pathPoint.Y)));
            }
            path.AddPolygon(pathPoints.ToArray());
            path.CloseFigure();
            region.MakeEmpty();
            region.Union(path);
            return(region.IsVisible(new System.Drawing.Point((int)Math.Round(point.X), (int)Math.Round(point.Y))));
        }