By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,017 Members | 1,011 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,017 IT Pros & Developers. It's quick & easy.

How to save InPtr to a raw file?

P: n/a
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
Apr 26 '06 #1
Share this Question
Share on Google+
15 Replies


P: n/a
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

"Sharon" <Sh*****@newsgroups.nospam> ha scritto nel messaggio
news:4A**********************************@microsof t.com...
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

Apr 26 '06 #2

P: n/a

"Sharon" <Sh*****@newsgroups.nospam> wrote in message
news:4A**********************************@microsof t.com...
| 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.
Apr 26 '06 #3

P: n/a
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" <Sh*****@newsgroups.nospam> wrote in message
news:4A**********************************@microsof t.com...
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

Apr 26 '06 #4

P: n/a
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 [MVP]" <wi*************@telenet.be> wrote in message
news:uX**************@TK2MSFTNGP04.phx.gbl...

"Sharon" <Sh*****@newsgroups.nospam> wrote in message
news:4A**********************************@microsof t.com...
| 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.

Apr 26 '06 #5

P: n/a
There are overloads for both from managed to unmanaged and unmanaged to
managed.

Willy.

"Stoitcho Goutsev (100)" <10*@100.com> wrote in message
news:Od**************@TK2MSFTNGP02.phx.gbl...
| 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 [MVP]" <wi*************@telenet.be> wrote in message
| news:uX**************@TK2MSFTNGP04.phx.gbl...
| >
| > "Sharon" <Sh*****@newsgroups.nospam> wrote in message
| > news:4A**********************************@microsof t.com...
| > | 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.
| >
| >
|
|
Apr 26 '06 #6

P: n/a
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)" <10*@100.com> wrote in message
news:Od**************@TK2MSFTNGP02.phx.gbl...
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 [MVP]" <wi*************@telenet.be> wrote in message
news:uX**************@TK2MSFTNGP04.phx.gbl...

"Sharon" <Sh*****@newsgroups.nospam> wrote in message
news:4A**********************************@microsof t.com...
| 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.


Apr 26 '06 #7

P: n/a
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)" <10*@100.com> wrote in message
news:Og**************@TK2MSFTNGP02.phx.gbl...
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" <Sh*****@newsgroups.nospam> wrote in message
news:4A**********************************@microsof t.com...
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


Apr 26 '06 #8

P: n/a
Still using V1.0? Marshal.Copy has overloads to copy from unmanaged to
managed since v1.1.

http://msdn.microsoft.com/library/de...scopytopic.asp

Willy.

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:Ov**************@TK2MSFTNGP03.phx.gbl...
| 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)" <10*@100.com> wrote in message
| news:Od**************@TK2MSFTNGP02.phx.gbl...
| > 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 [MVP]" <wi*************@telenet.be> wrote in message
| > news:uX**************@TK2MSFTNGP04.phx.gbl...
| >>
| >> "Sharon" <Sh*****@newsgroups.nospam> wrote in message
| >> news:4A**********************************@microsof t.com...
| >> | 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.
| >>
| >>
| >
| >
|
|
Apr 26 '06 #9

P: n/a
Yup, you are right. My apologies.

--

Stoitcho Goutsev (100)

"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
There are overloads for both from managed to unmanaged and unmanaged to
managed.

Willy.

"Stoitcho Goutsev (100)" <10*@100.com> wrote in message
news:Od**************@TK2MSFTNGP02.phx.gbl...
| 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 [MVP]" <wi*************@telenet.be> wrote in message
| news:uX**************@TK2MSFTNGP04.phx.gbl...
| >
| > "Sharon" <Sh*****@newsgroups.nospam> wrote in message
| > news:4A**********************************@microsof t.com...
| > | 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.
| >
| >
|
|

Apr 26 '06 #10

P: n/a
hi,

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

"Stoitcho Goutsev (100)" <10*@100.com> wrote in message
news:OR**************@TK2MSFTNGP04.phx.gbl...
Yup, you are right. My apologies.

--

Stoitcho Goutsev (100)

"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
There are overloads for both from managed to unmanaged and unmanaged to
managed.

Willy.

"Stoitcho Goutsev (100)" <10*@100.com> wrote in message
news:Od**************@TK2MSFTNGP02.phx.gbl...
| 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 [MVP]" <wi*************@telenet.be> wrote in message
| news:uX**************@TK2MSFTNGP04.phx.gbl...
| >
| > "Sharon" <Sh*****@newsgroups.nospam> wrote in message
| > news:4A**********************************@microsof t.com...
| > | 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.
| >
| >
|
|


Apr 26 '06 #11

P: n/a
>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.
Apr 26 '06 #12

P: n/a
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.

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:eg**************@TK2MSFTNGP04.phx.gbl...
| >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.
Apr 26 '06 #13

P: n/a
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.
------
Thanks
Sharon
Apr 27 '06 #14

P: n/a

"Sharon" <Sh*****@newsgroups.nospam> wrote in message
news:DD**********************************@microsof t.com...
| 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.
Apr 27 '06 #15

P: n/a
I think you are right Will. I'll use the PInvoke Win32 API's like "WriteFile"
or "WriteFileEx".
------
Thanks
Sharon
Apr 27 '06 #16

This discussion thread is closed

Replies have been disabled for this discussion.