user376591
user376591

Reputation:

Using native pointers in F# with WritableBitmap

Hello

I am trying to use WritableBitmap.BackBuffer as used in this example, see the section examples.
What I am trying to do really is write an int[] into a space a nativeint points to.

To be able to write from int [] to some memory I started from this answer on SO. Microsoft.FSharp.NativeInterop.NativePtr.write seems to be a good function to use to write.

After trying and reading a bit two questions arises.

  1. WritableBitmap.BackBuffer has the type nativeint, how to convert to nativeptr that NativePter.write wants?

  2. It seems that I can only write one int at a time but I want to write a whole int [].

I admit that I am in deep water but it is in the deep water you learn to swim :)

Thank in advance
Gorgen

Upvotes: 4

Views: 1458

Answers (2)

kvb
kvb

Reputation: 55195

I think Tomas's suggested approach makes sense. However, to answer your first question, you can convert a nativeint to a nativeptr using the NativeInterop.NativePtr.ofNativeInt function.

Upvotes: 3

Tomas Petricek
Tomas Petricek

Reputation: 243106

I think that the NativePtr.write function can be only used to write single value at a time, so if you want to copy an array, you'll have to use a for loop.

An easier option may be to use the Marshal.Copy method (see MSDN) which takes a source array (there are overloads for arrays containing elements of various types) and intptr as the destination.

Something like this should work:

let imageData = [| ... |] // generate one dimensional array with image data
writeableBitmap.Lock()

let buffer = writeableBitmap.BackBuffer
Marshal.Copy(imageData, 0, buffer, imageData.Length)

Upvotes: 3

Related Questions