如何将值放入图像数组?实际上由于bmpData.Stride,我无法在整个数组中执行此操作。存储值的字节大小应该在100左右,实际上是40。
我在使用System.Runtime.InteropServices.Marshal.Copy
时遇到了accessviolationexception。
我在MSDN Library - Bitmap.LockBits Method (Rectangle, ImageLockMode, PixelFormat)
的代码示例中使用为什么我不能写那样的东西?
// Declare an array to hold the bytes of the bitmap.
int bytes = Math.Abs(bmpData.Width) * b.Height;
我的整个代码是:
//Create new bitmap 10x10 = 100 pixels
Bitmap b = new Bitmap(10, 10, System.Drawing.Imaging.PixelFormat.Format1bppIndexed);
Rectangle rect = new Rectangle(0, 0, b.Width, b.Height);
System.Drawing.Imaging.BitmapData bmpData =
b.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
b.PixelFormat);
// Get the address of the first line.
IntPtr ptr = bmpData.Scan0;
// Declare an array to hold the bytes of the bitmap.
int bytes = Math.Abs(bmpData.Stride) * b.Height;//error if bmpData.Width
byte[] rgbValues = new byte[bytes];
// Copy the RGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
//Create random constructor
Random r = new Random();
//Generate dots in random cells and show image
for (int i = 0; i < bmpData.Height; i++)
{
for (int j = 0; j < b.Width; j++)
{
rgbValues[i + j] = (byte)r.Next(0, 2);
}
}
// Copy back values into the array.
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
// Unlock the bits.
b.UnlockBits(bmpData);
// Draw the modified image.
pictureBox1.Image = (Image)b;
答案 0 :(得分:8)
Format1bppIndexed
表示每个像素有一个位,而不是字节。此外,BMP格式要求每行以四字节边界开始。这是40
来自的地方:
要生成随机1bpp图像,您应该像这样重写循环:
// Generate dots in random cells and show image
for (int i = 0; i < bmpData.Height; i++)
{
for (int j = 0; j < bmpData.Width; j += 8)
{
rgbValues[i * bmpData.Stride + j / 8] = (byte)r.Next(0, 256);
}
}
或者只使用Random.NextBytes
方法而不是循环:
r.NextBytes(rgbValues);