3

Following Bob Powell's tutorial on LockBits, I put the following code into 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 pushing the Bitmap data into a picturebox (picturebox.Image = BitmapImage;) all that comes out is a red x over a white background, with a red border. What am I doing wrong?

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
GunnarJ
  • 440
  • 4
  • 12

1 Answers1

2

Have you forgotten to call UnlockBits at the end, as suggested at the end of this article: Using the LockBits method to access image data?

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
Ali Tarhini
  • 5,278
  • 6
  • 41
  • 66
  • Ya, now it works. Funny, I put that in before I asked this question, and the debugger wouldn't compile. It told me that only assignment, call, increment, etc. could be used as a statement. Thanks :) – GunnarJ Nov 28 '10 at 00:13