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

ToManagedImage() public method

Create managed image from the unmanaged.

If the makeCopy is set to , 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 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.

The unmanaged image has some invalid properties, which results /// in failure of converting it to managed image. This may happen if user used the /// constructor specifying some /// invalid parameters.
public ToManagedImage ( bool makeCopy ) : Bitmap
makeCopy bool Make a copy of the unmanaged image or not.
return System.Drawing.Bitmap
        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 ) ?
                        AForge.Imaging.Image.CreateGrayscaleImage( width, height ) :
                        new Bitmap( width, height, pixelFormat );

                    // lock destination bitmap data
                    BitmapData dstData = dstImage.LockBits(
                        new Rectangle( 0, 0, width, height ),
                        ImageLockMode.ReadWrite, pixelFormat );

                    int dstStride = dstData.Stride;
                    int lineSize  = Math.Min( stride, dstStride );

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

                        if ( stride != dstStride )
                        {
                            // copy image
                            for ( int y = 0; y < height; y++ )
                            {
                                AForge.SystemTools.CopyUnmanagedMemory( dst, src, lineSize );
                                dst += dstStride;
                                src += stride;
                            }
                        }
                        else
                        {
                            AForge.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." );
            }
        }

Same methods

UnmanagedImage::ToManagedImage ( ) : Bitmap

Usage Example

Beispiel #1
0
        private void BtnLoadimage_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog
            {
                Multiselect = true,
                Filter      = "Image files (*.png;*.jpeg;*.bmp)|*.png;*.jpeg;*.bmp|All files (*.*)|*.*",
                //openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                //InitialDirectory = "C:\\Users\\BrianWang\\Desktop\\English Muffin Scan\\Dec 1 2017"
                InitialDirectory = "C:\\Users\\kai23\\Projects\\ABI\\EnglishMuffinVision_AForge\\Images\\English Muffin\\Batch 1\\All Top"
            };

            if (openFileDialog.ShowDialog() == true)
            {
                foreach (string filename in openFileDialog.FileNames)
                {
                    lblfilename.Content = filename;

                    int    startPos = filename.LastIndexOf("SK Foods On-Site Scan") + "SK Foods On-Site Scan".Length + 1;
                    int    length   = filename.IndexOf("æ") - startPos - 1;
                    string sub      = filename.Substring(startPos, length);

                    lblFolder.Content = sub;

                    GrayScaleImage = AForge.Imaging.Image.FromFile(filename);
                    imageLoaded    = true;
                }
            }
            if (imageLoaded)
            {
                AForge.Imaging.UnmanagedImage unmanagedImage1 = AForge.Imaging.UnmanagedImage.FromManagedImage(GrayScaleImage);
                Bitmap      managedImage   = unmanagedImage1.ToManagedImage();
                BitmapImage GrayImage_temp = ToBitmapImage(managedImage);
                imgGray.Source = GrayImage_temp;
            }
        }
All Usage Examples Of AForge.Imaging.UnmanagedImage::ToManagedImage