473,804 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,lineLen gth];
IntPtr p = <hardwareDevice Class>.GetBufPt r();

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

thanks for help
Rainer
Dec 6 '06 #1
4 11979
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,lineLen gth];
IntPtr p = <hardwareDevice Class>.GetBufPt r();
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.ReadByt e(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-pleasedevexpres s.comschrieb im
Newsbeitrag news:c1******** *************** ***@news.micros oft.com...
How about copying to a single-dimensional array first?
This is what I am doing right now ...

//=========
byte[] tmpImage = new byte[numLine*lineLen gth];
List<byte[]imageLines = new List<byte[]>();
Marshal.Copy(fr ameGrabber.GetB ufPtr(deviceId) , tmpImage, 0,
numLine*lineLen gth);
for(int i = 0; i< numLine; i++)
{
byte[] line = new byte[lineLength];
Buffer.BlockCop y(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-pleasedevexpres s.comschrieb im
Newsbeitrag news:c1******** *************** ***@news.micros oft.com...
>How about copying to a single-dimensional array first?
This is what I am doing right now ...

//=========
byte[] tmpImage = new byte[numLine*lineLen gth];
List<byte[]imageLines = new List<byte[]>();
Marshal.Copy(fr ameGrabber.GetB ufPtr(deviceId) , tmpImage, 0,
numLine*lineLen gth);
for(int i = 0; i< numLine; i++)
{
byte[] line = new byte[lineLength];
Buffer.BlockCop y(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*lineLen gth];
byte[][] imageLines = new byte[numLine][];
Marshal.Copy(fr ameGrabber.GetB ufPtr(deviceId) , tmpImage, 0, numLine*lineLen gth);
for (int i = 0; i < numLine; i++)
{
imageLines[i] = new byte[lineLength];
Buffer.BlockCop y(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-pleasedevexpres s.comschrieb im
Newsbeitrag news:c1******** *************** ***@news.micros oft.com...
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*lineLen gth];
byte[][] imageLines = new byte[numLine][];
Marshal.Copy(fr ameGrabber.GetB ufPtr(deviceId) , tmpImage, 0,
numLine*lineLen gth);
for (int i = 0; i < numLine; i++)
{
imageLines[i] = new byte[lineLength];
Buffer.BlockCop y(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
10242
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 understand, because the integer indicates the size of the memory block that is required But the second version I do not understand. If I want a memoryblock of 20 bytes could I use IntPtr p = Marshal.AllocHGlobal ( new IntPtr ( 20 ) )
6
6698
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
2694
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 mixed mode DLL unmanaged code is written in unmanaged C++ & resides in an unmanaged DL Product used is VS.NET 200 Please tell me what is the best practice for getting and setting data fro unmanaged to managed part when I have a class with...
8
3327
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. However, the DEBUG_EVENT structure is defined as a union, and the size and contents vary depending on the event code contained in the header. typedef struct _DEBUG_EVENT { DWORD dwDebugEventCode; DWORD dwProcessId;
13
2172
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 code? Thanks PS I added the +1 because as I understand the GetLogicalDriveStrings doc
6
2723
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. System.Runtime.InteropServices.Marshal.WriteInt32 used to work fine but now is misbehaving. I wrote some native-code (C++) alternates to some Marshal methods and when I P/Invoke them they work fine; I can write to an address and I read back the same value that...
1
1321
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 of my String. I want to pass it as constant (LPCWSTR). I don't need allocation and copying, just the pointer. Can I have it, pretty please? In normal C++, we usually have conversion operator or a function to get it from a string class...
1
8313
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: ... IntPtr pResult; int len;
2
2313
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: // based on http://msdn.microsoft.com/en-us/magazine/bb985936.aspx std::string MarshalString (const System::String^ s) {
0
1405
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 = Marshal.AllocHGlobal(Marshal.SizeOf(GetType(Integer)) * myArray.Length) Marshal.Copy(myArray, 0, myPtr, myArray.Length)
0
9715
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10603
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10356
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9176
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7643
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4314
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.