Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.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# 将位图像素转换为字节数组失败_C#_Bitmap_Bytearray_Converter - Fatal编程技术网

C# 将位图像素转换为字节数组失败

C# 将位图像素转换为字节数组失败,c#,bitmap,bytearray,converter,C#,Bitmap,Bytearray,Converter,我必须将位图的像素转换为短数组。因此,我想: 获取字节 将字节转换为短字节 这是获取字节的源代码: public byte[] BitmapToByte(Bitmap source) { using (var memoryStream = new MemoryStream()) { source.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp); return me

我必须将位图的像素转换为短数组。因此,我想:

  • 获取字节
  • 将字节转换为短字节
这是获取字节的源代码:

 public byte[] BitmapToByte(Bitmap source)
 {
     using (var memoryStream = new MemoryStream())
     {
         source.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
         return memoryStream.ToArray();
     }
 }

这不会返回预期的结果。是否有其他方法转换数据?

请正确解释您的问题。“我缺少字节”不是可以解决的问题。你期望得到什么数据,你看到了什么

Bitmap.Save()
将根据指定的格式返回数据,在所有情况下,该格式包含的不仅仅是像素数据(描述宽度和高度的标题、颜色/调色板数据等)。如果您只需要像素数据数组,最好查看:


现在,
rgbValues
数组包含源位图中的所有像素,每个像素使用三个字节。我不知道你为什么想要一系列短裤,但你必须能从这里找到答案。

请正确解释你的问题。“我缺少字节”不是可以解决的问题。你期望得到什么数据,你看到了什么

Bitmap.Save()
将根据指定的格式返回数据,在所有情况下,该格式包含的不仅仅是像素数据(描述宽度和高度的标题、颜色/调色板数据等)。如果您只需要像素数据数组,最好查看:


现在,
rgbValues
数组包含源位图中的所有像素,每个像素使用三个字节。我不知道你为什么想要一系列的短裤,但你必须能从这里找到答案。

512 x 512 x 4=1048576
那么缺少的字节在哪里?
512 x 512 x 4=1048576
那么缺少的字节在哪里呢?@leppie我也不知道,但是,我现在必须处理整数值,但最终没有区别。thanks@leppie我也是,但是。我现在必须处理整数值,但最后没有区别。谢谢
Bitmap bmp = new Bitmap("c:\\fakePhoto.jpg");

// Lock the bitmap's bits.  
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.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) * bmp.Height;
byte[] rgbValues = new byte[bytes];

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