Ejemplo n.º 1
0
        /// <summary>
        ///     Converts <see cref="NDArray"/> to a <see cref="Bitmap"/>.
        /// </summary>
        /// <param name="nd">The <see cref="NDArray"/> to copy pixels from, <see cref="Shape"/> is ignored completely. If nd.Unsafe.Shape.IsContiguous == false then a copy is made.</param>
        /// <param name="width">The height of the <see cref="Bitmap"/></param>
        /// <param name="height">The width of the <see cref="Bitmap"/></param>
        /// <returns>A <see cref="Bitmap"/></returns>
        /// <exception cref="ArgumentException">When nd.size != width*height, which means the ndarray be turned into the given bitmap size.</exception>
        public static unsafe Bitmap ToBitmap(this NDArray nd, int width, int height)
        {
            if (nd == null)
            {
                throw new ArgumentNullException(nameof(nd));
            }

            if (width * height != nd.size)
            {
                throw new ArgumentException("Given nd.size != width*height");
            }

            if (!nd.Unsafe.Shape.IsContiguous)
            {
                nd = nd.Clone();
            }

            var ret     = new Bitmap(height, width);
            var bitdata = ret.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);

            try
            {
                nd.CopyTo(new UnmanagedMemoryBlock <byte>((byte *)bitdata.Scan0, bitdata.Stride * bitdata.Height));
            }
            finally
            {
                ret.UnlockBits(bitdata);
            }

            return(ret);
        }