예제 #1
0
        /// <summary>
        /// Create unmanaged image from the specified managed image.
        /// </summary>
        ///
        /// <param name="imageData">Source locked image data.</param>
        ///
        /// <returns>Returns new unmanaged image, which is a copy of source managed image.</returns>
        ///
        /// <remarks><para>The method creates an exact copy of specified managed image, but allocated
        /// in unmanaged memory. This means that managed image may be unlocked right after call to this
        /// method.</para></remarks>
        ///
        /// <exception cref="UnsupportedImageFormatException">Unsupported pixel format of source image.</exception>
        ///
        public static UnmanagedImage FromManagedImage(BitmapData imageData)
        {
            var pixelFormat = imageData.PixelFormat;

            // check source pixel format
            if (
                (pixelFormat != PixelFormat.Format8bppIndexed) &&
                (pixelFormat != PixelFormat.Format16bppGrayScale) &&
                (pixelFormat != PixelFormat.Format24bppRgb) &&
                (pixelFormat != PixelFormat.Format32bppRgb) &&
                (pixelFormat != PixelFormat.Format32bppArgb) &&
                (pixelFormat != PixelFormat.Format32bppPArgb) &&
                (pixelFormat != PixelFormat.Format48bppRgb) &&
                (pixelFormat != PixelFormat.Format64bppArgb) &&
                (pixelFormat != PixelFormat.Format64bppPArgb))
            {
                throw new UnsupportedImageFormatException("Unsupported pixel format of the source image.");
            }

            // allocate memory for the image
            var dstImageData = System.Runtime.InteropServices.Marshal.AllocHGlobal(imageData.Stride * imageData.Height);

            System.GC.AddMemoryPressure(imageData.Stride * imageData.Height);

            var image = new UnmanagedImage(dstImageData, imageData.Width, imageData.Height, imageData.Stride, pixelFormat);

            SystemTools.CopyUnmanagedMemory(dstImageData, imageData.Scan0, imageData.Stride * imageData.Height);
            image._mustBeDisposed = true;

            return(image);
        }
예제 #2
0
        /// <summary>
        /// Copy unmanaged image.
        /// </summary>
        ///
        /// <param name="destImage">Destination image to copy this image to.</param>
        ///
        /// <remarks><para>The method copies current unmanaged image to the specified image.
        /// Size and pixel format of the destination image must be exactly the same.</para></remarks>
        ///
        /// <exception cref="InvalidImagePropertiesException">Destination image has different size or pixel format.</exception>
        ///
        public void Copy(UnmanagedImage destImage)
        {
            if (
                (_width != destImage._width) || (_height != destImage._height) ||
                (_pixelFormat != destImage._pixelFormat))
            {
                throw new InvalidImagePropertiesException("Destination image has different size or pixel format.");
            }

            if (_stride == destImage._stride)
            {
                // copy entire image
                SystemTools.CopyUnmanagedMemory(destImage._imageData, _imageData, _stride * _height);
            }
            else
            {
                unsafe
                {
                    var dstStride  = destImage._stride;
                    var copyLength = (_stride < dstStride) ? _stride : dstStride;

                    var src = (byte *)_imageData.ToPointer();
                    var dst = (byte *)destImage._imageData.ToPointer();

                    // copy line by line
                    for (var i = 0; i < _height; i++)
                    {
                        SystemTools.CopyUnmanagedMemory(dst, src, copyLength);

                        dst += dstStride;
                        src += _stride;
                    }
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Clone the unmanaged images.
        /// </summary>
        ///
        /// <returns>Returns clone of the unmanaged image.</returns>
        ///
        /// <remarks><para>The method does complete cloning of the object.</para></remarks>
        ///
        public UnmanagedImage Clone()
        {
            // allocate memory for the image
            var newImageData = System.Runtime.InteropServices.Marshal.AllocHGlobal(_stride * _height);

            System.GC.AddMemoryPressure(_stride * _height);

            var newImage = new UnmanagedImage(newImageData, _width, _height, _stride, _pixelFormat)
            {
                _mustBeDisposed = true
            };

            SystemTools.CopyUnmanagedMemory(newImageData, _imageData, _stride * _height);

            return(newImage);
        }
예제 #4
0
        /// <summary>
        /// Clone image.
        /// </summary>
        ///
        /// <param name="sourceData">Source image data.</param>
        ///
        /// <returns>Clones image from source image data. The message does not clone pallete in the
        /// case if the source image has indexed pixel format.</returns>
        ///
        public static Bitmap Clone(BitmapData sourceData)
        {
            // get source image size
            var width  = sourceData.Width;
            var height = sourceData.Height;

            // create new image
            var destination = new Bitmap(width, height, sourceData.PixelFormat);

            // lock destination bitmap data
            var destinationData = destination.LockBits(
                new Rectangle(0, 0, width, height),
                ImageLockMode.ReadWrite, destination.PixelFormat);

            SystemTools.CopyUnmanagedMemory(destinationData.Scan0, sourceData.Scan0, height * sourceData.Stride);

            // unlock destination image
            destination.UnlockBits(destinationData);

            return(destination);
        }
예제 #5
0
        /// <summary>
        /// Create managed image from the unmanaged.
        /// </summary>
        ///
        /// <param name="makeCopy">Make a copy of the unmanaged image or not.</param>
        ///
        /// <returns>Returns managed copy of the unmanaged image.</returns>
        ///
        /// <remarks><para>If the <paramref name="makeCopy"/> is set to <see langword="true"/>, then the method
        /// creates a managed copy of the unmanaged image, so the managed image stays valid even when the unmanaged
        /// image gets disposed. However, setting this parameter to <see langword="false"/> creates a managed image which is
        /// just a wrapper around the unmanaged image. So if unmanaged image is disposed, the
        /// managed image becomes no longer valid and accessing it will generate an exception.</para></remarks>
        ///
        /// <exception cref="InvalidImagePropertiesException">The unmanaged image has some invalid properties, which results
        /// in failure of converting it to managed image. This may happen if user used the
        /// <see cref="UnmanagedImage(IntPtr, int, int, int, PixelFormat)"/> constructor specifying some
        /// invalid parameters.</exception>
        ///
        public Bitmap ToManagedImage(bool makeCopy)
        {
            Bitmap dstImage = null;

            try
            {
                if (!makeCopy)
                {
                    dstImage = new Bitmap(_width, _height, _stride, _pixelFormat, _imageData);
                    if (_pixelFormat == PixelFormat.Format8bppIndexed)
                    {
                        Image.SetGrayscalePalette(dstImage);
                    }
                }
                else
                {
                    // create new image of required format
                    dstImage = (_pixelFormat == PixelFormat.Format8bppIndexed) ?
                               Image.CreateGrayscaleImage(_width, _height) :
                               new Bitmap(_width, _height, _pixelFormat);

                    // lock destination bitmap data
                    var dstData = dstImage.LockBits(
                        new Rectangle(0, 0, _width, _height),
                        ImageLockMode.ReadWrite, _pixelFormat);

                    var dstStride = dstData.Stride;
                    var lineSize  = Math.Min(_stride, dstStride);

                    unsafe
                    {
                        var dst = (byte *)dstData.Scan0.ToPointer();
                        var src = (byte *)_imageData.ToPointer();

                        if (_stride != dstStride)
                        {
                            // copy image
                            for (var y = 0; y < _height; y++)
                            {
                                SystemTools.CopyUnmanagedMemory(dst, src, lineSize);
                                dst += dstStride;
                                src += _stride;
                            }
                        }
                        else
                        {
                            SystemTools.CopyUnmanagedMemory(dst, src, _stride * _height);
                        }
                    }

                    // unlock destination images
                    dstImage.UnlockBits(dstData);
                }

                return(dstImage);
            }
            catch (Exception)
            {
                if (dstImage != null)
                {
                    dstImage.Dispose();
                }

                throw new InvalidImagePropertiesException("The unmanaged image has some invalid properties, which results in failure of converting it to managed image.");
            }
        }