Goo*_*oot 1 c# bytearray converter bitmap
我必须将位图的像素转换为短阵列.因此我想:
这是获取字节的源码:
public byte[] BitmapToByte(Bitmap source)
{
using (var memoryStream = new MemoryStream())
{
source.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
return memoryStream.ToArray();
}
}
Run Code Online (Sandbox Code Playgroud)
这不会返回预期的结果.还有另一种转换数据的方法吗?
请妥善解释您的问题."我缺少字节"不是可以解决的问题.你期望什么数据,你看到了什么?
Bitmap.Save()
将根据指定的格式返回数据,在所有情况下,该格式不仅包含像素数据(描述宽度和高度的标题,颜色/调色板数据等).如果你只想要一个像素数据数组,你最好看看Bimap.LockBits()
:
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);
Run Code Online (Sandbox Code Playgroud)
现在,rgbValues
数组包含源位图中的所有像素,每个像素使用三个字节.我不知道为什么你想要一系列短裤,但你必须能够从这里弄明白.
归档时间: |
|
查看次数: |
2922 次 |
最近记录: |