Exchanging data between .Net application and WiT Engine requires the transfer of data between managed and unmanaged memory space. To do this, you need to use Marshal class static methods.
C# code may differ from Visual Basic because unsafe code is allowed in C#. This is an advantage of C#.
The following sample code demonstrates how you can get pixel values of an image (width×height unsigned 8-bit) produced by WiT Engine to your .Net application. In the callback method, the rawData argument is an unmanaged pointer.
[C#]
using System.Runtime.InteropServices;
unsafe
{
byte *p = (byte *)(void *)rawData;
byte value;
for (int i = 0; i < width * height; i++, p++) value = *p;
}
[Visual Basic]
Imports System.Runtime.InteropServices
Dim size As Integer = width * height
Dim val(size) As Byte
Marshal.Copy(rawData, val, 0, size)
Dim pixVal As Byte
For i = 0 To size - 1 Step 1
pixVal = val(i)
Next
The following sample code demonstrates how you can transfer pixel values of an image (width×height unsigned 8-bit) from your .Net application. to WiT Engine, using the SetInputData method. Notice that the memory allocated by Marshal.AllocHGlobal must be explicitly released by Marshal.FreeHGlobal.
[C#]
unsafe
{
byte * data = (byte *)Marshal.AllocHGlobal(width * height);
for (int i = 0; i < width; ++i)
for (int j = 0; j < height; ++j)
data[i*width + j] = (byte)j;
string header = String.Format("OBJ_B H CorImage CorUByte {0} {1}",
width, height);
WiT.engine.SetInputData(name, header, header.Length, (IntPtr)data,
width * height);
Marshal.FreeHGlobal((IntPtr)data);
}
[Visual Basic]
Dim size As Integer = width * height
Dim data As IntPtr = Marshal.AllocHGlobal(size)
Dim i As Integer
Dim j As Integer
Dim pos As Integer = 0
Dim val(size) As Byte
For i = 0 To width - 1 Step 1
For j = 0 To height - 1 Step 1
val(pos) = CType(j, Byte)
pos += 1
Next
Next
Marshal.Copy(val, 0, data, size)
Dim header As String = String.Format("OBJ_B H CorImage CorUByte {0} {1}",_
width, height)
WiT.engine.SetInputData(name, header, header.Length, CType(data, IntPtr),_
size)
Marshal.FreeHGlobal(CType(data, IntPtr))