Beispiel #1
0
 public Bitmap VectorsToBitmap(IRasterDataProvider prd, CodeCell.AgileMap.Core.Feature[] features, string shpPrimaryField, out Dictionary <string, Color> nameColors)
 {
     using (IVector2BitmapConverter c = new Vector2BitmapConverter())
     {
         Dictionary <ShapePolygon, Color> vectors = GetVectorColors(features, shpPrimaryField, out nameColors);
         Bitmap   bmp     = new Bitmap(prd.Width, prd.Height, PixelFormat.Format24bppRgb);
         Envelope envelop = GetEnvelop(prd);
         c.ToBitmap(vectors, Color.Black, envelop, new Size(prd.Width, prd.Height), ref bmp);
         return(bmp);
     }
 }
 private Bitmap GetBitmapUseRaster(PointF[] points, byte[] types, Envelope dstEnvelope, Size size)
 {
     if (dstEnvelope == null || size.IsEmpty)
     {
         return(null);
     }
     using (IVector2BitmapConverter c = new Vector2BitmapConverter())
     {
         Bitmap buffer = new Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb);
         c.ToBitmapUseRasterCoord(points, types, Color.Red, Color.Black, dstEnvelope, size, ref buffer);
         return(buffer);
     }
 }
        /// <summary>
        /// 解决了大图像AOI栅格化内存溢出的问题
        /// 解决方法:
        /// 1、按照AOI的最小外包矩形栅格化计算AOI索引
        /// 2、将计算完成的AOI索引平移到大图
        /// </summary>
        /// <param name="rasterPoints"></param>
        /// <param name="types"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public int[] GetAOI(PointF[] rasterPoints, byte[] types, Size size)
        {
            Envelope evp = new Envelope(0, 0, size.Width, size.Height);

            using (IVector2BitmapConverter c = new Vector2BitmapConverter())
            {
                int    offsetX, offsetY;
                Size   newSize;
                Bitmap buffer = GetBitmapUseRasterII(rasterPoints, types, evp, size, out offsetX, out offsetY, out newSize);
                if (newSize.IsEmpty || buffer == null)
                {
                    return(null);
                }
                using (IBitmap2RasterConverter rstc = new Bitmap2RasterConverter())
                {
                    int[] aoi = rstc.ToRaster(buffer, Color.Red);
                    if (aoi == null || aoi.Length == 0)
                    {
                        return(null);
                    }
                    if (aoi != null)
                    {
                        Array.Sort <int>(aoi);
                    }
                    int        count = aoi.Length;
                    int        newWidth = newSize.Width;
                    int        oldWidth = size.Width;
                    int        oldHeight = size.Height;
                    int        row = 0, col = 0;
                    List <int> newAio = new List <int>();
                    for (int i = 0; i < count; i++)
                    {
                        row  = aoi[i] / newWidth;
                        col  = aoi[i] % newWidth;
                        row += offsetY;
                        col += offsetX;
                        if (col < 0 || col >= oldWidth || row < 0 || row >= oldHeight)
                        {
                            continue;
                        }
                        else
                        {
                            newAio.Add(row * oldWidth + col);
                        }
                    }
                    return(newAio.ToArray());
                }
            }
        }
 public int[] GetAOI(PointF[] coordPoints, byte[] types, Envelope dstEnvelope, Size size)
 {
     if (dstEnvelope == null || size.IsEmpty)
     {
         return(null);
     }
     using (IVector2BitmapConverter c = new Vector2BitmapConverter())
     {
         Bitmap buffer = GetBitmap(coordPoints, types, dstEnvelope, size);
         using (IBitmap2RasterConverter rstc = new Bitmap2RasterConverter())
         {
             return(rstc.ToRaster(buffer, Color.Red));
         }
     }
 }
 private Bitmap GetBitmap(ShapePolygon[] geometrys, Envelope dstEnvelope, Size size)
 {
     if (dstEnvelope == null || size.IsEmpty)
     {
         return(null);
     }
     using (IVector2BitmapConverter c = new Vector2BitmapConverter())
     {
         Bitmap buffer = new Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb);
         Dictionary <ShapePolygon, Color> vectors = new Dictionary <ShapePolygon, Color>();
         foreach (ShapePolygon geo in geometrys)
         {
             vectors.Add(geo, Color.Red);
         }
         c.ToBitmap(vectors, Color.Black, dstEnvelope, size, ref buffer);
         return(buffer);
     }
 }
        public int[] GetAOI_OLD(PointF[] rasterPoints, byte[] types, Size size)
        {
            Envelope evp = new Envelope(0, 0, size.Width, size.Height);

            using (IVector2BitmapConverter c = new Vector2BitmapConverter())
            {
                Bitmap buffer = GetBitmapUseRaster(rasterPoints, types, evp, size);
                using (IBitmap2RasterConverter rstc = new Bitmap2RasterConverter())
                {
                    int[] aoi = rstc.ToRaster(buffer, Color.Red);
                    if (aoi != null)
                    {
                        Array.Sort <int>(aoi);
                    }
                    return(aoi);
                }
            }
        }
        /// <summary>
        /// 函数GetBitmapUseRasterII解决大图像栅格化出现内存溢出的错误
        /// 解决方法:
        /// 1、先按照AOI最小外包矩形栅格化;
        /// 2、将栅格化后的索引平移至大图像。
        /// </summary>
        /// <param name="points"></param>
        /// <param name="types"></param>
        /// <param name="dstEnvelope"></param>
        /// <param name="size"></param>
        /// <param name="offsetX"></param>
        /// <param name="offsetY"></param>
        /// <param name="newSize"></param>
        /// <returns></returns>
        private Bitmap GetBitmapUseRasterII(PointF[] points, byte[] types, Envelope dstEnvelope, Size size,
                                            out int offsetX, out int offsetY, out Size newSize)
        {
            offsetX = offsetY = 0;
            newSize = Size.Empty;
            if (dstEnvelope == null || size.IsEmpty)
            {
                return(null);
            }
            //
            float minX = float.MaxValue;
            float maxX = int.MinValue;
            float minY = float.MaxValue;
            float maxY = float.MinValue;

            for (int i = 0; i < points.Length; i++)
            {
                if (points[i].X < minX)
                {
                    minX = points[i].X;
                }
                if (points[i].X > maxX)
                {
                    maxX = points[i].X;
                }
                if (points[i].Y < minY)
                {
                    minY = points[i].Y;
                }
                if (points[i].Y > maxY)
                {
                    maxY = points[i].Y;
                }
            }
            //
            offsetX = (int)minX;
            offsetY = (int)minY;

            int width  = (int)(maxX - minX);
            int height = (int)(maxY - minY);

            if (width <= 0 || height <= 0)
            {
                return(null);
            }
            newSize = new Size(width, height);
            for (int i = 0; i < points.Length; i++)
            {
                points[i].X -= minX;
                points[i].Y -= minY;
            }
            //
            double   xSpan       = dstEnvelope.Width / size.Width;
            double   ySpan       = dstEnvelope.Height / size.Height;
            double   minXCoord   = dstEnvelope.MinX + offsetX * xSpan;
            double   maxYCoord   = dstEnvelope.MaxY - offsetY * ySpan;
            double   maxXCoord   = minXCoord + width * xSpan;
            double   minYCoord   = maxYCoord - height * ySpan;
            Envelope newEnvelope = new Envelope(minXCoord, minYCoord, maxXCoord, maxYCoord);

            //offsetY = Math.Abs(offsetY);
            //
            using (IVector2BitmapConverter c = new Vector2BitmapConverter())
            {
                Bitmap buffer = new Bitmap(width, height, PixelFormat.Format24bppRgb);
                c.ToBitmapUseRasterCoord(points, types, Color.Red, Color.Black, newEnvelope, newSize, ref buffer);
                return(buffer);
            }
        }