How to save InPtr to a raw file?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello gurus,

I have a System.IntPtr pointing to the memory address of my image (not a
..NET image).
I wish to save this image data pointed by the IntPtr to a raw data file. The
way I now to that is:

System.IO.FileStream file = new System.IO.FileStream(fileName,
System.IO.FileMode.Create);
file.Write(byte[], 0, ...);
file.Close();

But the FileStream.Write takes byte[] which I do not have.
How can I save this image data pointed by IntPtr to file?
 
Is your IntPtr a bitmap handle (hBitmap) or a direct pointer to raw data?
If it is, you could construct an Image.FromHBitmap and then serialize the
image class.
Otherwise you need to convert it to a byte[], for example by means of
System.Runtime.InteropServices.Marshal.ReadIntPtr or like.

Laura
 
| Hello gurus,
|
| I have a System.IntPtr pointing to the memory address of my image (not a
| .NET image).
| I wish to save this image data pointed by the IntPtr to a raw data file.
The
| way I now to that is:
|
| System.IO.FileStream file = new System.IO.FileStream(fileName,
| System.IO.FileMode.Create);
| file.Write(byte[], 0, ...);
| file.Close();
|
| But the FileStream.Write takes byte[] which I do not have.
| How can I save this image data pointed by IntPtr to file?
|
|
|
| ---------
| Thanks
| Sharon

You can use Marshal.Copy to copy the memory contents to a byte[].

Willy.
 
Sharon,

The fact that you are using IntPtr makes me believe that your data is in
unmanaged memory. To get the data in managed byte array you can use
System.Runtime.InteropServices.Marshal class.
You can use the Marshal.ReadByte methods to loop through the data in
unmanaged memory and copy it one byte at a time in a managed byte[] or you
can create a structure with byte array in it and use Marshal.PtrToStructure
method to read it at once. The latter I haven't tried, but should work.
 
Willy,

Isn't it Marshal.Copy copy from managed to unmanaged? In other words from
byte[] to memory pointed by IntPtr. I think Sharon needs the other way
around.
 
There are overloads for both from managed to unmanaged and unmanaged to
managed.

Willy.

| Willy,
|
| Isn't it Marshal.Copy copy from managed to unmanaged? In other words from
| byte[] to memory pointed by IntPtr. I think Sharon needs the other way
| around.
|
|
| --
|
| Stoitcho Goutsev (100)
|
| | >
| > | > | Hello gurus,
| > |
| > | I have a System.IntPtr pointing to the memory address of my image (not
a
| > | .NET image).
| > | I wish to save this image data pointed by the IntPtr to a raw data
file.
| > The
| > | way I now to that is:
| > |
| > | System.IO.FileStream file = new System.IO.FileStream(fileName,
| > | System.IO.FileMode.Create);
| > | file.Write(byte[], 0, ...);
| > | file.Close();
| > |
| > | But the FileStream.Write takes byte[] which I do not have.
| > | How can I save this image data pointed by IntPtr to file?
| > |
| > |
| > |
| > | ---------
| > | Thanks
| > | Sharon
| >
| > You can use Marshal.Copy to copy the memory contents to a byte[].
| >
| > Willy.
| >
| >
|
|
 
Hi,

You are correct, Copy is to send data to unmanaged


--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Stoitcho Goutsev (100) said:
Willy,

Isn't it Marshal.Copy copy from managed to unmanaged? In other words from
byte[] to memory pointed by IntPtr. I think Sharon needs the other way
around.


--

Stoitcho Goutsev (100)

Willy Denoyette said:
| Hello gurus,
|
| I have a System.IntPtr pointing to the memory address of my image (not
a
| .NET image).
| I wish to save this image data pointed by the IntPtr to a raw data
file.
The
| way I now to that is:
|
| System.IO.FileStream file = new System.IO.FileStream(fileName,
| System.IO.FileMode.Create);
| file.Write(byte[], 0, ...);
| file.Close();
|
| But the FileStream.Write takes byte[] which I do not have.
| How can I save this image data pointed by IntPtr to file?
|
|
|
| ---------
| Thanks
| Sharon

You can use Marshal.Copy to copy the memory contents to a byte[].

Willy.
 
Hi,

I find hard to believe there is no way to copy a chunk of data from
unmanaged memory, I was under the impression that Copy worked both way ,
It's not it can only be used from managed to unmanaged.

I haven't been able to find the other way around though.

Stoitcho Goutsev (100) said:
Sharon,

The fact that you are using IntPtr makes me believe that your data is in
unmanaged memory. To get the data in managed byte array you can use
System.Runtime.InteropServices.Marshal class.
You can use the Marshal.ReadByte methods to loop through the data in
unmanaged memory and copy it one byte at a time in a managed byte[] or you
can create a structure with byte array in it and use
Marshal.PtrToStructure method to read it at once. The latter I haven't
tried, but should work.


--
HTH
Stoitcho Goutsev (100)
Sharon said:
Hello gurus,

I have a System.IntPtr pointing to the memory address of my image (not a
.NET image).
I wish to save this image data pointed by the IntPtr to a raw data file.
The
way I now to that is:

System.IO.FileStream file = new System.IO.FileStream(fileName,
System.IO.FileMode.Create);
file.Write(byte[], 0, ...);
file.Close();

But the FileStream.Write takes byte[] which I do not have.
How can I save this image data pointed by IntPtr to file?
 
Still using V1.0? Marshal.Copy has overloads to copy from unmanaged to
managed since v1.1.

http://msdn.microsoft.com/library/d...ntimeinteropservicesmarshalclasscopytopic.asp

Willy.

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message | Hi,
|
| You are correct, Copy is to send data to unmanaged
|
|
| --
| Ignacio Machin,
| ignacio.machin AT dot.state.fl.us
| Florida Department Of Transportation
|
| | > Willy,
| >
| > Isn't it Marshal.Copy copy from managed to unmanaged? In other words
from
| > byte[] to memory pointed by IntPtr. I think Sharon needs the other way
| > around.
| >
| >
| > --
| >
| > Stoitcho Goutsev (100)
| >
| > | >>
| >> | >> | Hello gurus,
| >> |
| >> | I have a System.IntPtr pointing to the memory address of my image
(not
| >> a
| >> | .NET image).
| >> | I wish to save this image data pointed by the IntPtr to a raw data
| >> file.
| >> The
| >> | way I now to that is:
| >> |
| >> | System.IO.FileStream file = new System.IO.FileStream(fileName,
| >> | System.IO.FileMode.Create);
| >> | file.Write(byte[], 0, ...);
| >> | file.Close();
| >> |
| >> | But the FileStream.Write takes byte[] which I do not have.
| >> | How can I save this image data pointed by IntPtr to file?
| >> |
| >> |
| >> |
| >> | ---------
| >> | Thanks
| >> | Sharon
| >>
| >> You can use Marshal.Copy to copy the memory contents to a byte[].
| >>
| >> Willy.
| >>
| >>
| >
| >
|
|
 
hi,

Sorry, you are right , i did not check the entire list of overloads :)



Stoitcho Goutsev (100) said:
Yup, you are right. My apologies.

--

Stoitcho Goutsev (100)

Willy Denoyette said:
There are overloads for both from managed to unmanaged and unmanaged to
managed.

Willy.

| Willy,
|
| Isn't it Marshal.Copy copy from managed to unmanaged? In other words
from
| byte[] to memory pointed by IntPtr. I think Sharon needs the other way
| around.
|
|
| --
|
| Stoitcho Goutsev (100)
|
| | >
| > | > | Hello gurus,
| > |
| > | I have a System.IntPtr pointing to the memory address of my image
(not
a
| > | .NET image).
| > | I wish to save this image data pointed by the IntPtr to a raw data
file.
| > The
| > | way I now to that is:
| > |
| > | System.IO.FileStream file = new System.IO.FileStream(fileName,
| > | System.IO.FileMode.Create);
| > | file.Write(byte[], 0, ...);
| > | file.Close();
| > |
| > | But the FileStream.Write takes byte[] which I do not have.
| > | How can I save this image data pointed by IntPtr to file?
| > |
| > |
| > |
| > | ---------
| > | Thanks
| > | Sharon
| >
| > You can use Marshal.Copy to copy the memory contents to a byte[].
| >
| > Willy.
| >
| >
|
|
 
I don't have the V1 docs handy, but I remember this was not in the initial
drops (could be in the early V1.0 beta drops), but I could be wrong as well.

Willy.

| >Still using V1.0? Marshal.Copy has overloads to copy from unmanaged to
| >managed since v1.1.
|
| Since 1.0 even.
|
|
| Mattias
|
| --
| Mattias Sjögren [C# MVP] mattias @ mvps.org
| http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
| Please reply only to the newsgroup.
 
I'm not using unmanaged memory, I'm simply using a 3'rd party library
(Atalasoft DotImage) that has a AtalaImage object, and this object hold an
image data that I need as raw data fro processing, and the only API it has
for that is the IntPtr (or other API that do copy).
The solution you all gave me should work fine, but they all do COPY which I
very much wish to avoid.

Assuming the AtalaImage object holds it data in a managed memory, I wish to
find a way just to hold that same memory address but as byte[] to be able to
save it in a file using the FileStream.Write(...). Or maybe using other way
to save this data held by the IntPtr as a raw data file.
 
| I'm not using unmanaged memory, I'm simply using a 3'rd party library

Which is using unmanaged memory for the image data, it's simply not possible
otherwise.

| (Atalasoft DotImage) that has a AtalaImage object, and this object hold an
| image data that I need as raw data fro processing, and the only API it has
| for that is the IntPtr (or other API that do copy).
| The solution you all gave me should work fine, but they all do COPY which
I
| very much wish to avoid.

You can't avoid the copy, unless you resort to unmanaged API's for writing.

|
| Assuming the AtalaImage object holds it data in a managed memory,

It does not, no single graphics package can hold it's raw image data in the
managed GC heap, they all have to use unmanaged GDI/GDI+ to get at the data
and they all will have to copy to managed memory before they can pass the
data to managed API's.


I wish to
| find a way just to hold that same memory address but as byte[] to be able
to
| save it in a file using the FileStream.Write(...). Or maybe using other
way
| to save this data held by the IntPtr as a raw data file.

In this case you'll need to PInvoke Win32 API's "WriteFile" or
"WriteFileEx".


Willy.
 
I think you are right Will. I'll use the PInvoke Win32 API's like "WriteFile"
or "WriteFileEx".
 
Back
Top