简体   繁体   中英

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. They also give me a uint called dataSize that corresponds to the length of 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) . Naturally, if I try to use data as a parameter to Write(...) , it tells me I cannot convert from byte* to 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 .

What other options do I have available to me to take this byte* pointer and write it to a file? 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).

Take a look at the UnmanagedMemoryStream type:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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