Пример #1
0
        private void DrawLine(BoundingRectangle rectangle, Color color, Point p0, Point p1)
        {
            int x0 = p0.X;
            int y0 = p0.Y;
            int x1 = p1.X;
            int y1 = p1.Y;

            var rectMinPixel = MapAroundControl.MapToClient(rectangle.Min);
            var rectMaxPixel = MapAroundControl.MapToClient(rectangle.Max);
            int bitmapWidth  = Math.Abs(rectMinPixel.X - rectMaxPixel.X);
            int bitmapHeight = Math.Abs(rectMinPixel.Y - rectMaxPixel.Y);

            int minX = Math.Min(p0.X, p1.X);
            int minY = Math.Min(p0.Y, p1.Y);
            int maxX = Math.Max(p0.X, p1.X);
            int maxY = Math.Max(p0.Y, p1.Y);

            if (bitmapWidth == 0)
            {
                bitmapWidth = 1;
            }
            if (bitmapHeight == 0)
            {
                bitmapHeight = 1;
            }

            Bitmap      bitmap = new Bitmap(bitmapWidth, bitmapHeight);
            RasterLayer layer;

            layer       = new RasterLayer();
            layer.Alias = $"region_line_{_rasterLayers.Count}";
            _rasterLayers.Add(layer);
            _mapAroundMap.AddLayer(layer);

            layer.AddRasterPreview(bitmap, rectangle, bitmap.Width, bitmap.Height);
            layer.Visible = true;


            //Rectangle lockRect = new Rectangle(minX, minY, maxX - minX, maxY - minY);
            Rectangle  lockRect   = new Rectangle(0, 0, bitmapWidth, bitmapHeight);
            BitmapData bitmapData = bitmap.LockBits(lockRect, ImageLockMode.WriteOnly, bitmap.PixelFormat);

            byte[] pixelBytes = { color.B, color.G, color.R, color.A };

            bool PlotPoint(int x, int y)
            {
                int plotX = x;
                int plotY = y;

                if (plotX < 0 || plotY < 0 || plotX >= bitmap.Width || plotY >= bitmap.Height)
                {
                    return(true);
                }

                int positionByte = (y * bitmap.Width + x) * pixelBytes.Length;

                Marshal.Copy(pixelBytes, 0, bitmapData.Scan0 + positionByte, pixelBytes.Length);
                //bitmap.SetPixel(plotX, plotY, color);
                return(true);
            }

            BresenhamLinePlotter plotter = new BresenhamLinePlotter();

            plotter.CastLine(new Vector2Int(x0, y0), new Vector2Int(x1, y1), PlotPoint);

            bitmap.UnlockBits(bitmapData);

            MapAroundControl.RedrawMap();
        }