System.Drawing.Bitmap.LockBits C# (CSharp) Method

LockBits() public method

public LockBits ( RectangleF rect, ImageLockMode flags, PixelFormat pixelFormat ) : BitmapData
rect RectangleF
flags ImageLockMode
pixelFormat PixelFormat
return System.Drawing.Imaging.BitmapData
        public BitmapData LockBits(RectangleF rect, ImageLockMode flags, PixelFormat pixelFormat)
        {
            BitmapData bitmapData = new BitmapData ();

            if (!ConversionHelpers.sTablesInitialized)
                ConversionHelpers.CalculateTables ();

            // Calculate our strides
            int srcStride = (int)((int)rect.Width * (NativeCGImage.BitsPerPixel / NativeCGImage.BitsPerComponent));

            int numOfComponents = GetPixelFormatComponents(pixelFormat);
            int stride = (int)rect.Width * numOfComponents;

            // Calculate our lengths
            int srcScanLength  = (int)(Math.Abs(srcStride) * rect.Height);
            int scanLength = (int)(Math.Abs(stride) * rect.Height);

            // Declare an array to hold the scan bytes of the bitmap.
            byte[] scan0 = new byte[scanLength];
            pinnedScanArray = GCHandle.Alloc(scan0, GCHandleType.Pinned);
            bitmapData.Scan0 = pinnedScanArray.AddrOfPinnedObject();

            byte[] srcScan0 = new byte[srcScanLength];

            IntPtr ptr = bitmapBlock;
            if (ptr == IntPtr.Zero)
            {
                var pData = NativeCGImage.DataProvider;
                var nData = pData.CopyData ();
                ptr = nData.Bytes;
            }
            // Copy the RGB values into the scan array.
            System.Runtime.InteropServices.Marshal.Copy(ptr, srcScan0, 0, srcScanLength);

            if (numOfComponents == 4)
                Convert_P_RGBA_8888_To_BGRA_8888 (ref scan0, srcScan0);
            else
                Convert_P_RGBA_8888_To_BGR_888 (ref scan0, srcScan0);

            // We need to support sub rectangles.
            if (rect != new RectangleF (new PointF (0, 0), physicalDimension))
            {
                throw new NotImplementedException("Sub rectangles of bitmaps not supported yet.");
            }
            else
            {
                bitmapData.Height = (int)rect.Height;
                bitmapData.Width = (int)rect.Width;
                bitmapData.PixelFormat = pixelFormat;

                bitmapData.Stride = stride;
            }

            return bitmapData;
        }

Usage Example

		public static int LoadTexture(string filename)
		{
			var bitmap = new Bitmap (filename);

			int id = GL.GenTexture ();

			BitmapData bmpData = bitmap.LockBits (
				new Rectangle (0, 0, bitmap.Width, bitmap.Height),
				ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

			GL.BindTexture (TextureTarget.Texture2D, id);

			GL.TexImage2D (TextureTarget.Texture2D, 0,
				PixelInternalFormat.Rgba,
				bitmap.Width, bitmap.Height, 0,
				OpenTK.Graphics.OpenGL.PixelFormat.Bgra,
				PixelType.UnsignedByte,
				bmpData.Scan0);

			bitmap.UnlockBits (bmpData);

			GL.TexParameter (TextureTarget.Texture2D,
				TextureParameterName.TextureMinFilter, 
				(int)TextureMinFilter.Linear);

			GL.TexParameter (TextureTarget.Texture2D,
				TextureParameterName.TextureMagFilter, 
				(int)TextureMagFilter.Linear);

			return id;
		}
All Usage Examples Of System.Drawing.Bitmap::LockBits