Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 创建1Bppined图像,步幅和accessviolationexception有问题_C#_Image Processing_Marshalling_Access Violation - Fatal编程技术网

C# 创建1Bppined图像,步幅和accessviolationexception有问题

C# 创建1Bppined图像,步幅和accessviolationexception有问题,c#,image-processing,marshalling,access-violation,C#,Image Processing,Marshalling,Access Violation,如何将值放入图像数组中?实际上,由于bmpData.Stride,我无法在整个阵列中执行此操作。存储值的字节大小应该在100左右,实际上是40 我在使用System.Runtime.InteropServices.marshall.Copy时收到accessviolationexception 我在代码示例中使用了 为什么我不能写那样的东西 // Declare an array to hold the bytes of the bitmap. int bytes = Math.

如何将值放入图像数组中?实际上,由于bmpData.Stride,我无法在整个阵列中执行此操作。存储值的字节大小应该在100左右,实际上是40

我在使用
System.Runtime.InteropServices.marshall.Copy
时收到accessviolationexception

我在代码示例中使用了

为什么我不能写那样的东西

// 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;
//创建新位图10x10=100像素
位图b=新位图(10,10,System.Drawing.Imaging.PixelFormat.Format1BPindexed);
矩形rect=新矩形(0,0,b.宽度,b.高度);
System.Drawing.Imaging.BitmapData bmpData=
b、 锁位(rect、System.Drawing.Imaging.ImageLockMode.ReadWrite、,
b、 像素格式);
//获取第一行的地址。
IntPtr ptr=bmpData.Scan0;
//声明一个数组以保存位图的字节。
int bytes=Math.Abs(bmpData.Stride)*b.高度//如果bmpData.Width
字节[]rgbValues=新字节[字节];
//将RGB值复制到数组中。
System.Runtime.InteropServices.Marshal.Copy(ptr,rgbvalue,0,字节);
//创建随机构造函数
随机r=新随机();
//在随机单元格中生成点并显示图像
对于(int i=0;i
format1bpindexed
表示每个像素有一个位,而不是字节。此外,BMP格式要求每行从四字节边界开始。这就是
40
的来源:

  • [10像素行]x[1位/像素]=10位=2字节
  • 行大小应该是4的倍数,因此每行将追加4-2=2个字节
  • [10行]x[4字节/行]=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);
        }
    }
    
    r.NextBytes(rgbValues);