PC Review


Reply
Thread Tools Rate Thread

Byte Pointer in VB

 
 
Turbot
Guest
Posts: n/a
 
      11th Mar 2005
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

 
Reply With Quote
 
 
 
 
Mattias Sjögren
Guest
Posts: n/a
 
      11th Mar 2005

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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
 
Reply With Quote
 
Turbot
Guest
Posts: n/a
 
      14th Mar 2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A hint on marshalling a byte array into a void* pointer Beorne Microsoft C# .NET 3 30th May 2007 02:01 PM
How to convert form Byte pointer to Int16 pointer Howard Weiss Microsoft VC .NET 2 7th Jul 2004 10:06 AM
Re: passing BYTE array pointer in vb.net Geoff Schwab [MSFT] Microsoft Dot NET Compact Framework 0 5th Feb 2004 07:10 PM
create bitmap from byte* - pointer Soja Cender Microsoft Dot NET Compact Framework 1 20th Aug 2003 07:01 PM
Convert native byte array (pointer) to managed byte[] Dave Microsoft Dot NET 1 13th Aug 2003 05:08 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:46 PM.