Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.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#-图像到Int[]-元素数错误_C# - Fatal编程技术网

C#-图像到Int[]-元素数错误

C#-图像到Int[]-元素数错误,c#,C#,我想将位图图像转换为Int数组,这样我就可以计算该数组中所有元素的总和,并进行其他数学计算 使用Forge.NET,我提取了绿色通道并获得了图像的裁剪版本。裁剪图像的尺寸为:1989 x 115=228735像素。使用Lockbits和Marshal.Copy转换为int数组后,数组的维数为229080;这超出了它的预期;下面是我的代码,请务必就我所犯的错误向我提供建议 // create filter ExtractBiggestBlob BBB = new ExtractBiggestBlo

我想将位图图像转换为Int数组,这样我就可以计算该数组中所有元素的总和,并进行其他数学计算

使用Forge.NET,我提取了绿色通道并获得了图像的裁剪版本。裁剪图像的尺寸为:1989 x 115=228735像素。使用Lockbits和Marshal.Copy转换为int数组后,数组的维数为229080;这超出了它的预期;下面是我的代码,请务必就我所犯的错误向我提供建议

// create filter
ExtractBiggestBlob BBB = new ExtractBiggestBlob();
// apply the filter
Bitmap biggestBlobsImage = BBB.Apply(ChannelImage); // works perfect; set all pictureBox to Zoom size
pictureBox1.Image = biggestBlobsImage; // pictureBox1 - GreenChannelCroppedImage
Console.WriteLine(biggestBlobsImage.Width);
Console.WriteLine(biggestBlobsImage.Height);
// Next step - Display rotation corrected image in pictureBox3
// Convert the Bitmap biggestBlobsImage to Int array;
Rectangle rect = new Rectangle(0, 0, biggestBlobsImage.Width, biggestBlobsImage.Height);
System.Drawing.Imaging.BitmapData bmpData =
  biggestBlobsImage.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, biggestBlobsImage.PixelFormat);

IntPtr ptr = bmpData.Scan0;
int bytes = Math.Abs(bmpData.Stride) * biggestBlobsImage.Height;
byte[] rgbValues = new byte[bytes];

// Copy the RGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

// do something with the array
Console.WriteLine(rgbValues.Length);
Console.WriteLine(rgbValues.Rank);

// Copy the RGB values back to the bitmap
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);

biggestBlobsImage.UnlockBits(bmpData);

请注意,您是按高度正确复制步幅,而不是按高度复制宽度。您必须使用Stride,因为当您的每个像素为24位,每个像素需要3个字节时,那么在32位内存中,您可以在每行末尾有几个未使用的字节。
例如,如果您有1像素乘以1像素的图像,而您只需要24位,但您需要32位,因为您必须获取整个字(32位系统中为32位,或64位内存系统中为64位)

假设您已经阅读了用于计算大小的属性说明,那么不清楚您为什么会期望奇数字节……感谢n00b向noob解释:)