C#: LockBits puts out a Giant Red X
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开发者_C百科);
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?
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?
精彩评论