AForge.Imaging.UnmanagedImage.Create C# (CSharp) Method

Create() public static method

Allocate new image in unmanaged memory.

Allocate new image with specified attributes in unmanaged memory.

The method supports only Format8bppIndexed, Format16bppGrayScale, Format24bppRgb, Format32bppRgb, Format32bppArgb, Format32bppPArgb, Format48bppRgb, Format64bppArgb and Format64bppPArgb pixel formats. In the case if Format8bppIndexed format is specified, pallete is not not created for the image (supposed that it is 8 bpp grayscale image).

Unsupported pixel format was specified. Invalid image size was specified.
public static Create ( int width, int height, PixelFormat pixelFormat ) : UnmanagedImage
width int Image width.
height int Image height.
pixelFormat PixelFormat Image pixel format.
return UnmanagedImage
        public static UnmanagedImage Create( int width, int height, PixelFormat pixelFormat )
        {
            int 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
            int stride = width * bytesPerPixel;

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

            // allocate memory for the image
            IntPtr imageData = System.Runtime.InteropServices.Marshal.AllocHGlobal( stride * height );
            AForge.SystemTools.SetUnmanagedMemory( imageData, 0, stride * height );

            UnmanagedImage image = new UnmanagedImage( imageData, width, height, stride, pixelFormat );
            image.mustBeDisposed = true;

            return image;
        }

Usage Example

Beispiel #1
0
        public unsafe void ExtractBlobsImage(UnmanagedImage image, Blob blob, bool extractInOriginalSize)
        {
            if (objectLabels == null)
            {
                throw new ApplicationException("Image should be processed before to collect objects map.");
            }
            if (image.PixelFormat != PixelFormat.Format24bppRgb && image.PixelFormat != PixelFormat.Format8bppIndexed && image.PixelFormat != PixelFormat.Format32bppRgb && image.PixelFormat != PixelFormat.Format32bppArgb && image.PixelFormat != PixelFormat.Format32bppRgb && image.PixelFormat != PixelFormat.Format32bppPArgb)
            {
                throw new UnsupportedImageFormatException("Unsupported pixel format of the provided image.");
            }
            int width   = image.Width;
            int height  = image.Height;
            int stride  = image.Stride;
            int num     = System.Drawing.Image.GetPixelFormatSize(image.PixelFormat) / 8;
            int width2  = blob.Rectangle.Width;
            int height2 = blob.Rectangle.Height;
            int width3  = extractInOriginalSize ? width : width2;
            int height3 = extractInOriginalSize ? height : height2;
            int left    = blob.Rectangle.Left;
            int num2    = left + width2 - 1;
            int top     = blob.Rectangle.Top;
            int num3    = top + height2 - 1;
            int iD      = blob.ID;

            blob.Image        = UnmanagedImage.Create(width3, height3, image.PixelFormat);
            blob.OriginalSize = extractInOriginalSize;
            byte *ptr  = (byte *)image.ImageData.ToPointer() + (long)top * (long)stride + (long)left * (long)num;
            byte *ptr2 = (byte *)blob.Image.ImageData.ToPointer();
            int   num4 = top * width + left;

            if (extractInOriginalSize)
            {
                ptr2 += top * blob.Image.Stride + left * num;
            }
            int num5 = stride - width2 * num;
            int num6 = blob.Image.Stride - width2 * num;
            int num7 = width - width2;

            for (int i = top; i <= num3; i++)
            {
                int num8 = left;
                while (num8 <= num2)
                {
                    if (objectLabels[num4] == iD)
                    {
                        *ptr2 = *ptr;
                        if (num > 1)
                        {
                            ptr2[1] = ptr[1];
                            ptr2[2] = ptr[2];
                            if (num > 3)
                            {
                                ptr2[3] = ptr[3];
                            }
                        }
                    }
                    num8++;
                    num4++;
                    ptr2 += num;
                    ptr  += num;
                }
                ptr  += num5;
                ptr2 += num6;
                num4 += num7;
            }
        }
All Usage Examples Of AForge.Imaging.UnmanagedImage::Create