473,396 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Marshal.Copy IntPtr -> byte[,]

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
Dec 6 '06 #1
4 11906
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.
Dec 6 '06 #2
Hi Dustin,

thanks for your comments.

"Dustin Campbell" <du*****@no-spam-pleasedevexpress.comschrieb im
Newsbeitrag news:c1**************************@news.microsoft.c om...
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
Dec 6 '06 #3
Hi Dustin,
>
thanks for your comments.

"Dustin Campbell" <du*****@no-spam-pleasedevexpress.comschrieb im
Newsbeitrag news:c1**************************@news.microsoft.c om...
>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[i] = new byte[lineLength];
Buffer.BlockCopy(tmpImage, i * lineLength, imageLines[i], 0, lineLength);
}

Best Regards,
Dustin Campbell
Developer Express Inc.
Dec 6 '06 #4
Hi Dustin,

"Dustin Campbell" <du*****@no-spam-pleasedevexpress.comschrieb im
Newsbeitrag news:c1**************************@news.microsoft.c om...
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[i] = new byte[lineLength];
Buffer.BlockCopy(tmpImage, i * lineLength, imageLines[i], 0, lineLength);
}
This is how I will do it.

Regards
Rainer
Dec 7 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stefan | last post by:
I have the following question The Marshal class contains a function to allocate a block of memory 1. Marshal.AllocHGlobal ( int cb 2. Marshal.AllocHGlobal ( IntPtr cb The first version I can...
6
by: Howard Kaikow | last post by:
Given: private struct PROCESSENTRY32 { public int dwSize; public int cntUsage; public int th32ProcessID; public int th32DefaultHeapID; public int th32ModuleID;
0
by: Johannes Unfried | last post by:
Problem Best practice needed to marshal STL data from managed code to unmanaged code & vice vers Details managed code is written in managed C++ & accesses the unmanaged code (i.e. lives in a...
8
by: Charles Law | last post by:
Can anyone suggest how I would marshal a variable length structure back from an API call. Specifically, I am looking at the WaitForDebugEvent function, which returns a DEBUG_EVENT structure. ...
13
by: Just Me | last post by:
The following almost works. The problem is Marshal.PtrToStringAuto seems to terminate at the first null so I don't get the full string. Any suggestions on how to fix this? Or how to improve the...
6
by: william.thorpe.b | last post by:
I have recently switched from VS2003 to VS2005 and at the same time from V1 to V2 of the .NET Compact Framework. The target is a Windows CE 5.0 device and an ARMV4I processor. ...
1
by: Goran | last post by:
Hi all! I need to pass managed String from to C-style APIs. I see I can use Marshal::StringToXXX functions. Is this the best we have? I understand this will allocate a new string and create copy...
1
by: dast | last post by:
I would need a function to copy unsigned short values from a IntPtr to a ushort-array, but the Marshal.Copy-function only support the short-array! At the moment I do it the following way: ......
2
by: Kenneth Porter | last post by:
I try to be const correct everywhere and found that I can't pass a const String to this API. Is it going to change the value? Or is the signature not the best it could be? I want to write this:...
0
by: Gareth | last post by:
If I want to move a signed integer array to unmanaged memory I can use the following code: Dim myArray() As Integer Dim myPtr As IntPtr myPtr =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.