【发布时间】:2014-08-16 14:56:47
【问题描述】:
我试图弄清楚如何使用 USB Video Camera API,这有点不太清楚(至少它似乎依赖于开发人员以前的知识,我不认为我理解“全貌” ,双关语)。
我有以下成功接收数据的事件处理程序,我目前只能打印到控制台:
void _camera_OnPreviewBitmap(IntPtr pbyteBitmap, uint dwBufferSize,
uint dwWidth, uint dwHeight,
uint dwFrameNo, uint dwPreviewPixelFormat) {
Console.WriteLine(string.Format("{0}, {1}, {2}, {3}, {4}, {5}", pbyteBitmap, dwBufferSize, dwWidth, dwHeight, dwFrameNo, dwPreviewPixelFormat));
//The callback function gets the image data as “IntPtr” type.
//When using “System.Runtime.InteropServices.Marshal.Copy”, the image data can copy to the array.
//When using “System.Runtime.InteropServices.Marchal.ReadByte”, the image data can get thedirectory.
//Sometimes the process speed is fast when receiving the image data after the image data copy to the array with
//“System.Runtime.InteropServices.Marchal.Copy”.
}
这给了我这些结果:
259129344, 1228800, 1280, 960, 195051, 1
250281984, 1228800, 1280, 960, 195052, 1
259129344, 1228800, 1280, 960, 195053, 1
250281984, 1228800, 1280, 960, 195054, 1
259129344, 1228800, 1280, 960, 195055, 1
250281984, 1228800, 1280, 960, 195056, 1
259129344, 1228800, 1280, 960, 195057, 1
250281984, 1228800, 1280, 960, 195058, 1
259129344, 1228800, 1280, 960, 195059, 1
250281984, 1228800, 1280, 960, 195060, 1
259129344, 1228800, 1280, 960, 195061, 1
250281984, 1228800, 1280, 960, 195062, 1
因此,这与图像尺寸 (1280 x 960) 一致,并且 1228800 是像素数(缓冲区大小),像素格式的 1 表示 PIXEL_FORMAT_08_MONO_OR_RAW 根据相机 API 像素格式枚举(其中也和这个BW相机一致)。
我的问题是:
如何获取这些信息并使用它创建一个
System.Drawing.Bitmap对象?
我已经尝试过了,但没有成功(显然灰度图像实际上没有被索引...):
var bmp = new System.Drawing.Bitmap((int)dwWidth, (int)dwHeight, (int)dwBufferSize,
PixelFormat.Format8bppIndexed, pbyteBitmap);
这最终会产生“通用 GDI+ 错误”。
【问题讨论】:
-
@Steve 如果我没记错的话,链接的问题不适用,因为我有一个 IntPtr 作为输入,而不是一个字节数组。一个共同点是我们都在处理相机 API,但我不认为这些问题是真正的重复。
标签: c# marshalling system.drawing interopservices