Byte Pointer in VB

  • Thread starter Thread starter Turbot
  • Start date Start date
T

Turbot

Hello,

I have come across this bit of code in a C# project and want to try and
replicate the behaviour in VB:

System.IntPtr ptrScan0 = objBMData.Scan0;
System.IntPtr ptrSrcScan0 = objBMSrc.Scan0;

byte * pOrig = (byte *)(void *)ptrScan0;
byte * pSrc = (byte *)(void *)ptrSrcScan0;

I know that VB does not support byte pointers but there must be a way
of doing this. The code then goes on to do this:

for(int y=0;y < yVal;++y)
{
for(int x=0; x < xVal; ++x )
{
pOrig[0] = pSrc[newVal1];
pOrig[1] = pSrc[newVal2];
pOrig[2] = pSrc[newVal3];
pOrig += 3;
}
pOrig += nOffset;
}

I may be wrong (i've never been a C programmer) but this looks like we
are setting three values in memory based on the current position of the
pointer and then shifting the start position of the pointer by three
through each iteration of the inner for loop. Again, I would need to
know how to do this in VB.NET.

Your help is much appreciated.

IAN KEVAN
 
You can use Marshal.Copy to copy the data from the memory pointed to
by the IntPtr to a local byte array, do your manipulations, and then
copy it back.



Mattias
 
Hi Mattias,

Thanks for that. I had already tried Marshal.Copy but it hadn't worked
because I was setting the byte array size incorrectly. After your
message I figured I would try again and this time I have got it to
work. The VB code now looks like this:

Dim ptrScan0 As IntPtr = objBMData.Scan0
Dim ptrSrcScan0 As IntPtr = objBMSrc.Scan0

Dim pOrig(CorrectArrayLength) As Byte
Call Marshal.Copy(ptrScan0, pOrig, 0, CorrectArrayLength)
Dim pSrc(CorrectArrayLength) As Byte
Call Marshal.Copy(ptrSrcScan0, pSrc, 0, CorrectArrayLength)

Dim intCount As Integer = 0
For intY As Integer = 0 To intHeight - 1
For intX As Integer = 0 To intWidth - 1
p(intCount) = pSrc(intNewVal)
p(intCount + 1) = pSrc(intNewVal + 1)
p(intCount + 2) = pSrc(intNewVal + 2)
intCount = intCount + 3
Next
intCount = intCount + intOffset
Next

Call Marshal.Copy(pOrig, 0, ptrScan0, CorrectArrayLength)

It is considerably slower that the unsafe C# code but faster than
altering the pixels using SetPixel and GetPixel.

Thanks again,

IAN
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top