C #: LockBits releases giant red X

Following Bob Powell’s LockBits tutorial , I introduced the following code in C # 2010 Visual Studio Express:

System.Drawing.Imaging.BitmapData bmp = 
    BitmapImage
        .LockBits(new Rectangle(0, 0, 800, 600),
                  System.Drawing.Imaging.ImageLockMode.ReadWrite, 
                  MainGrid.PixelFormat)

        unsafe
        {
            for (int y = 0; y < bmp.Height; y++)
            {
                byte* row = (byte*)bmp.Scan0 + (y * bmp.Stride);
                for (int x = 0; x < bmp.Width; x++)
                {
                    row[x * 4] = 255;
                }
            }
        }

After clicking the bitmap data into the picture box (picturebox.Image = BitmapImage;) all that comes out is red x on a white background with a red frame. What am I doing wrong?

+3
source share
1 answer

You forgot to call UnlockBitsat the end, as suggested at the end of the link http://www.bobpowell.net/lockingbits.htm

+2
source

Source: https://habr.com/ru/post/1777238/


All Articles