问题描述
我有一个C#.NET库,抓住帧由一个摄像头。我需要将这些帧发送到本机应用程序,从获取图像无符号字符*
。
I have a C# .NET library that grabs frames from a camera. I need to send those frames to a native application that takes images from an unsigned char*
.
我最初把帧作为系统::图纸::位图
。
到目前为止,我可以检索字节[]
从位图
。我的测试是,分辨率400 * 234的图像,我应该得到400 * 234 * 3个字节得到一个RGB图像需要24 bpp的。
So far I can retrieve a byte[]
from the Bitmap
. My test is done with an image of resolution 400*234, I should be getting 400*234*3 bytes to get to the 24bpp a RGB image requires.
不过,我得到一个字节[]
尺寸11948的。
However, I'm getting a byte[]
of size 11948.
这是我从位图
到字节[]
如何转换:
private static byte[] ImageToByte(Bitmap img)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
什么是正确的方法来从系统::图纸::位图
转换为RGB 无符号字符*
?
What is the proper way to convert from System::Drawing::Bitmap
to RGB unsigned char*
?
推荐答案
这必须使用lockBits方法来完成,这里是一个code例如:
This has to be done using the lockBits method, here is a code example:
Rectangle rect = new Rectangle(0, 0, m_bitmap.Width, m_bitmap.Height);
BitmapData bmpData = m_bitmap.LockBits(rect, ImageLockMode.ReadOnly,
m_bitmap.PixelFormat);
IntPtr ptr = bmpData.Scan0;
int bytes = Math.Abs(bmpData.Stride) * m_bitmap.Height;
byte[] rgbValues = new byte[bytes];
Marshal.Copy(ptr, rgbValues, 0, bytes);
m_bitmap.UnlockBits(bmpData);
GCHandle handle = GCHandle::Alloc(rgbValues, GCHandleType::Pinned);
unsigned char * data = (unsigned char*) (void*) handle.AddrOfPinnedObject();
//do whatever with data
这篇关于系统:绘制::位图到无符号字符*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!