Exemplo n.º 1
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);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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.");
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Allocate new image in unmanaged memory.
        /// </summary>
        ///
        /// <param name="width">Image width.</param>
        /// <param name="height">Image height.</param>
        /// <param name="pixelFormat">Image pixel format.</param>
        ///
        /// <returns>Return image allocated in unmanaged memory.</returns>
        ///
        /// <remarks><para>Allocate new image with specified attributes in unmanaged memory.</para>
        ///
        /// <para><note>The method supports only
        /// <see cref="System.Drawing.Imaging.PixelFormat">Format8bppIndexed</see>,
        /// <see cref="System.Drawing.Imaging.PixelFormat">Format16bppGrayScale</see>,
        /// <see cref="System.Drawing.Imaging.PixelFormat">Format24bppRgb</see>,
        /// <see cref="System.Drawing.Imaging.PixelFormat">Format32bppRgb</see>,
        /// <see cref="System.Drawing.Imaging.PixelFormat">Format32bppArgb</see>,
        /// <see cref="System.Drawing.Imaging.PixelFormat">Format32bppPArgb</see>,
        /// <see cref="System.Drawing.Imaging.PixelFormat">Format48bppRgb</see>,
        /// <see cref="System.Drawing.Imaging.PixelFormat">Format64bppArgb</see> and
        /// <see cref="System.Drawing.Imaging.PixelFormat">Format64bppPArgb</see> pixel formats.
        /// In the case if <see cref="System.Drawing.Imaging.PixelFormat">Format8bppIndexed</see>
        /// format is specified, pallete is not not created for the image (supposed that it is
        /// 8 bpp grayscale image).
        /// </note></para>
        /// </remarks>
        ///
        /// <exception cref="UnsupportedImageFormatException">Unsupported pixel format was specified.</exception>
        /// <exception cref="InvalidImagePropertiesException">Invalid image size was specified.</exception>
        ///
        public static UnmanagedImage Create(int width, int height, PixelFormat pixelFormat)
        {
            var bytesPerPixel = 0;

            // calculate bytes per pixel
            switch (pixelFormat)
            {
            case PixelFormat.Format8bppIndexed:
                bytesPerPixel = 1;
                break;

            case PixelFormat.Format16bppGrayScale:
                bytesPerPixel = 2;
                break;

            case PixelFormat.Format24bppRgb:
                bytesPerPixel = 3;
                break;

            case PixelFormat.Format32bppRgb:
            case PixelFormat.Format32bppArgb:
            case PixelFormat.Format32bppPArgb:
                bytesPerPixel = 4;
                break;

            case PixelFormat.Format48bppRgb:
                bytesPerPixel = 6;
                break;

            case PixelFormat.Format64bppArgb:
            case PixelFormat.Format64bppPArgb:
                bytesPerPixel = 8;
                break;

            default:
                throw new UnsupportedImageFormatException("Can not create image with specified pixel format.");
            }

            // check image size
            if ((width <= 0) || (height <= 0))
            {
                throw new InvalidImagePropertiesException("Invalid image size specified.");
            }

            // calculate stride
            var stride = width * bytesPerPixel;

            if (stride % 4 != 0)
            {
                stride += (4 - (stride % 4));
            }

            // allocate memory for the image
            var imageData = System.Runtime.InteropServices.Marshal.AllocHGlobal(stride * height);

            SystemTools.SetUnmanagedMemory(imageData, 0, stride * height);
            System.GC.AddMemoryPressure(stride * height);

            var image = new UnmanagedImage(imageData, width, height, stride, pixelFormat)
            {
                _mustBeDisposed = true
            };

            return(image);
        }