0
我似乎无法将我的大脑包围图像并将其从原始RGB颜色的byte[]
转换为BitMap
。我发现了一个解决方案,它允许我使用SetPixel
将RGB 24bpp byte[]
转换为BitMap
,但我读过使用LockBits
的速度要快得多,所以我正试图弄清楚如何以这种方式进行。RGB 24到BitMap造成黑色图像
使用SetPixel
方法,我正在用倒置图像:
public static Bitmap CreateBitmap24bppRgb(byte[] bmpData, int width, int height)
{
var bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
var pos = 0;
for (var y = 0; y < height; y++)
{
for (var x = 0; x < width; x++)
{
bmp.SetPixel(x, y, Color.FromArgb(bmpData[pos], bmpData[pos + 1], bmpData[pos + 2]));
pos += 3;
}
}
return bmp;
}
,我可以不太似乎弄清楚如何反转。但是当我尝试使用LockBits
时,图像只是黑色,我不确定我做错了什么,它看起来很直截了当。
public static Bitmap CreateBitmap24bppRgb(byte[] data, int width, int height)
{
var bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
//Create a BitmapData and Lock all pixels to be written
var bmpData = bmp.LockBits(
new Rectangle(0, 0, bmp.Width, bmp.Height),
ImageLockMode.WriteOnly, bmp.PixelFormat);
//Copy the data from the byte array into BitmapData.Scan0
for (int y = 0; y < bmp.Height - 1; y++)
{
Marshal.Copy(data, y * bmp.Width, bmpData.Scan0 bmpData.Stride);
}
//Unlock the pixels
bmp.UnlockBits(bmpData);
return bmp;
}
我只是好奇这里怎么了?
位图数据是* *通常存储倒置。你的Marshal.Copy()方法看起来像无稽之谈,邮政编码实际上可以编译。第三个参数应该是bmpData.Scan0 +(bmp.Height - y - 1)* bmpData.Stride。 – 2014-10-08 15:16:54
除了Hans的评论:在lockBits期间,你究竟用'data'做了什么?你将不得不__set__一些像素的渠道那里..!看例如[这里](http://stackoverflow.com/questions/26224095/how-to-find-the-difference-between-two-images/26225153#26225153)为一个工作lockBits代码的例子。当然还有更多... – TaW 2014-10-08 15:30:57