简体   繁体   English

在C#中,如何使用指向FileStream的byte *指针编写数组而不使用Marshal复制它?

[英]In C#, how do I write a array with a byte* pointer to a FileStream without copying it using Marshal?

I am using a camera SDK from the manufacturer, and they are giving me a class called Image with a pointer called data of type byte* that points to the beginning of the image data. 我正在使用制造商生产的相机SDK,他们给了我一个名为Image的类,其中一个名为data byte* data指针指向图像数据的开头。 They also give me a uint called dataSize that corresponds to the length of data . 他们还给了我一个名为dataSizeuint ,它对应于data的长度。

Now, I want to write this into a file using FileStream, but unfortunately the only write function in FileStream is of signature Write(Byte[], Int32, Int32) . 现在,我想使用FileStream将其写入文件,但不幸的是,FileStream中唯一的写入函数是签名Write(Byte[], Int32, Int32) Naturally, if I try to use data as a parameter to Write(...) , it tells me I cannot convert from byte* to byte[] . 当然,如果我尝试使用data作为Write(...)的参数,它告诉我我不能从byte*转换为byte[]

Since in my application, performance is critical, I want to avoid copying the data into a byte[] array using Marshal.Copy(...) as suggested in this answer . 因为在我的应用程序中,性能至关重要,我想避免使用Marshal.Copy(...)将数据复制到byte[]数组中,如本答案所示

What other options do I have available to me to take this byte* pointer and write it to a file? 我可以使用哪些其他选项来获取此byte*指针并将其写入文件? Note that my overall goal is to take these images, which are coming in thousands per second, and write them to disk as fast as possible (ie sequentially for an SSD). 请注意,我的总体目标是拍摄这些每秒数千张的图像,并尽快将它们写入磁盘(即依次为SSD)。

Take a look at the UnmanagedMemoryStream type: 看一下UnmanagedMemoryStream类型:

Image cameraData = YourSDKFunction();
using (var camera = new UnmanagedMemoryStream(cameraData.data, (int64)cameraData.dataSize))
using (var outFile = new FileStream("outputFile.jpg"))
{
     camera.CopyTo(outFile);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM