Marshal.Copy IntPtr -> byte[,]

R

Rainer Queck

Hi NG,

is there a way to copy a buffer pointed to by a IntPtr directly into a two
dimensional byte-array?

I tried this, what obviously doesn't work:

byte[,] image = new byte[numLine,lineLength];
IntPtr p = <hardwareDeviceClass>.GetBufPtr();

Marshal.Copy(p,image,0,numLine*lineLength);

thanks for help
Rainer
 
D

Dustin Campbell

Hi Rainer,
is there a way to copy a buffer pointed to by a IntPtr directly into a
two dimensional byte-array?

I tried this, what obviously doesn't work:

byte[,] image = new byte[numLine,lineLength];
IntPtr p = <hardwareDeviceClass>.GetBufPtr();
Marshal.Copy(p,image,0,numLine*lineLength);

How about copying to a single-dimensional array first?

byte[] imageData = new byte[numLines * lineLength];
Marshal.Copy(p, imageData, 0, numLines * lineLength);

byte[,] image = new byte[numLines, lineLength];
for (int i = 0; i < numLines; i++)
for (int j = 0; j < lineLength; j++)
image[i, j] = imageData[(i * lineLength) + j];

Or, you could read each byte separately but that would be slower:

byte[,] image = new byte[numLines * lineLength];
for (int i = 0; i < numLines; i++)
for (int j = 0; i < lineLength; j++)
image[i, j] = Marshal.ReadByte(p, (i * lineLength) + j);

Best Regards,
Dustin Campbell
Developer Express Inc.
 
R

Rainer Queck

Hi Dustin,

thanks for your comments.

How about copying to a single-dimensional array first?
This is what I am doing right now ...

//=========
byte[] tmpImage = new byte[numLine*lineLength];
List<byte[]> imageLines = new List<byte[]>();
Marshal.Copy(frameGrabber.GetBufPtr(deviceId), tmpImage, 0,
numLine*lineLength);
for(int i = 0; i< numLine; i++)
{
byte[] line = new byte[lineLength];
Buffer.BlockCopy(tmpImage, i * lineLength, line, 0, lineLength);
imageLines.Add(line);
}
//=========

But the application ist quit time critical, and I wanted to do the Copy(s)
as fast as posible.
Right now (current hardware with current code) it takes me 3 mSek which is
quite acceptable, but faster would be better....

Rainer
 
D

Dustin Campbell

Hi Dustin,
thanks for your comments.

How about copying to a single-dimensional array first?
This is what I am doing right now ...

//=========
byte[] tmpImage = new byte[numLine*lineLength];
List<byte[]> imageLines = new List<byte[]>();
Marshal.Copy(frameGrabber.GetBufPtr(deviceId), tmpImage, 0,
numLine*lineLength);
for(int i = 0; i< numLine; i++)
{
byte[] line = new byte[lineLength];
Buffer.BlockCopy(tmpImage, i * lineLength, line, 0, lineLength);
imageLines.Add(line);
}
//=========

But the application ist quit time critical, and I wanted to do the
Copy(s)
as fast as posible.
Right now (current hardware with current code) it takes me 3 mSek
which is
quite acceptable, but faster would be better....

Since you know the number of lines in advance, you might use a jagged array
instead of a List<byte[]> like so:

byte[] tmpImage = new byte[numLine*lineLength];
byte[][] imageLines = new byte[numLine][];
Marshal.Copy(frameGrabber.GetBufPtr(deviceId), tmpImage, 0, numLine*lineLength);
for (int i = 0; i < numLine; i++)
{
imageLines = new byte[lineLength];
Buffer.BlockCopy(tmpImage, i * lineLength, imageLines, 0, lineLength);
}

Best Regards,
Dustin Campbell
Developer Express Inc.
 
R

Rainer Queck

Hi Dustin,

Since you know the number of lines in advance, you might use a jagged
array
Thanks for this hint. Jagged arrays did not cross my way up today and they
sound like what I need.
instead of a List<byte[]> like so:

byte[] tmpImage = new byte[numLine*lineLength];
byte[][] imageLines = new byte[numLine][];
Marshal.Copy(frameGrabber.GetBufPtr(deviceId), tmpImage, 0,
numLine*lineLength);
for (int i = 0; i < numLine; i++)
{
imageLines = new byte[lineLength];
Buffer.BlockCopy(tmpImage, i * lineLength, imageLines, 0, lineLength);
}

This is how I will do it.

Regards
Rainer
 

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

Top