How do we keep the underlying byte[] from being garbage collected.

Oct 30, 2011 at 2:11 AM
How is pBits locked? Won't the GC collect or move pBits after the fixed() statement is finished, causing the Bitmap to corrupt managed memory?

unsafe
                {
                    fixed ( byte* pBits = bits )
                    {
                        IntPtr ptr = new IntPtr ( pBits );

                        System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap (
                            width,
                            height,
                            stride,
                            System.Drawing.Imaging.PixelFormat.Format32bppPArgb,
                            ptr );

                        picture.Image = bitmap;
                    }
                }