473,725 Members | 2,281 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 11977
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
10232
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
6687
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
3324
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
2165
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
2720
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
1314
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
8217
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
2309
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
1401
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
8889
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
9257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9116
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8099
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...
0
6011
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
4519
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...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.