Home All Groups Group Topic Archive Search About

How to save InPtr to a raw file?

Author
26 Apr 2006 12:36 PM
Sharon
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

Author
26 Apr 2006 2:13 PM
Laura T.
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

Show quoteHide quote
"Sharon" <SharonG@newsgroups.nospam> ha scritto nel messaggio
news:4AFA28FC-CF37-43CE-A9D7-5A27240D0E6C@microsoft.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
Are all your drivers up to date? click for free checkup

Author
26 Apr 2006 2:19 PM
Willy Denoyette [MVP]
Show quote Hide quote
"Sharon" <SharonG@newsgroups.nospam> wrote in message
news:4AFA28FC-CF37-43CE-A9D7-5A27240D0E6C@microsoft.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.
Author
26 Apr 2006 3:29 PM
Stoitcho Goutsev (100)
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)

Show quoteHide quote
"Willy Denoyette [MVP]" <willy.denoye***@telenet.be> wrote in message
news:uX9$3xTaGHA.4416@TK2MSFTNGP04.phx.gbl...
>
> "Sharon" <SharonG@newsgroups.nospam> wrote in message
> news:4AFA28FC-CF37-43CE-A9D7-5A27240D0E6C@microsoft.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.
>
>
Author
26 Apr 2006 3:38 PM
Willy Denoyette [MVP]
There are overloads for both from managed to unmanaged and unmanaged to
managed.

Willy.

Show quoteHide quote
"Stoitcho Goutsev (100)" <1**@100.com> wrote in message
news:OdPkiWUaGHA.5004@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]" <willy.denoye***@telenet.be> wrote in message
| news:uX9$3xTaGHA.4416@TK2MSFTNGP04.phx.gbl...
| >
| > "Sharon" <SharonG@newsgroups.nospam> wrote in message
| > news:4AFA28FC-CF37-43CE-A9D7-5A27240D0E6C@microsoft.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.
| >
| >
|
|
Author
26 Apr 2006 4:47 PM
Stoitcho Goutsev (100)
Yup, you are right. My apologies.

--

Stoitcho Goutsev (100)

Show quoteHide quote
"Willy Denoyette [MVP]" <willy.denoye***@telenet.be> wrote in message
news:%23DEaEeUaGHA.4144@TK2MSFTNGP04.phx.gbl...
> There are overloads for both from managed to unmanaged and unmanaged to
> managed.
>
> Willy.
>
> "Stoitcho Goutsev (100)" <1**@100.com> wrote in message
> news:OdPkiWUaGHA.5004@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]" <willy.denoye***@telenet.be> wrote in message
> | news:uX9$3xTaGHA.4416@TK2MSFTNGP04.phx.gbl...
> | >
> | > "Sharon" <SharonG@newsgroups.nospam> wrote in message
> | > news:4AFA28FC-CF37-43CE-A9D7-5A27240D0E6C@microsoft.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.
> | >
> | >
> |
> |
>
>
Author
26 Apr 2006 5:10 PM
Ignacio Machin ( .NET/ C# MVP )
hi,

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



Show quoteHide quote
"Stoitcho Goutsev (100)" <1**@100.com> wrote in message
news:ORLXuBVaGHA.4620@TK2MSFTNGP04.phx.gbl...
> Yup, you are right. My apologies.
>
> --
>
> Stoitcho Goutsev (100)
>
> "Willy Denoyette [MVP]" <willy.denoye***@telenet.be> wrote in message
> news:%23DEaEeUaGHA.4144@TK2MSFTNGP04.phx.gbl...
>> There are overloads for both from managed to unmanaged and unmanaged to
>> managed.
>>
>> Willy.
>>
>> "Stoitcho Goutsev (100)" <1**@100.com> wrote in message
>> news:OdPkiWUaGHA.5004@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]" <willy.denoye***@telenet.be> wrote in message
>> | news:uX9$3xTaGHA.4416@TK2MSFTNGP04.phx.gbl...
>> | >
>> | > "Sharon" <SharonG@newsgroups.nospam> wrote in message
>> | > news:4AFA28FC-CF37-43CE-A9D7-5A27240D0E6C@microsoft.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.
>> | >
>> | >
>> |
>> |
>>
>>
>
>
Author
26 Apr 2006 3:39 PM
Ignacio Machin ( .NET/ C# MVP )
Hi,

You are correct, Copy is to send data to unmanaged


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

Show quoteHide quote
"Stoitcho Goutsev (100)" <1**@100.com> wrote in message
news:OdPkiWUaGHA.5004@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]" <willy.denoye***@telenet.be> wrote in message
> news:uX9$3xTaGHA.4416@TK2MSFTNGP04.phx.gbl...
>>
>> "Sharon" <SharonG@newsgroups.nospam> wrote in message
>> news:4AFA28FC-CF37-43CE-A9D7-5A27240D0E6C@microsoft.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.
>>
>>
>
>
Author
26 Apr 2006 3:48 PM
Willy Denoyette [MVP]
Still using V1.0? Marshal.Copy has overloads to copy from unmanaged to
managed since v1.1.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemruntimeinteropservicesmarshalclasscopytopic.asp

Willy.

Show quoteHide quote
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:OvZRLeUaGHA.1200@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)" <1**@100.com> wrote in message
| news:OdPkiWUaGHA.5004@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]" <willy.denoye***@telenet.be> wrote in message
| > news:uX9$3xTaGHA.4416@TK2MSFTNGP04.phx.gbl...
| >>
| >> "Sharon" <SharonG@newsgroups.nospam> wrote in message
| >> news:4AFA28FC-CF37-43CE-A9D7-5A27240D0E6C@microsoft.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.
| >>
| >>
| >
| >
|
|
Author
26 Apr 2006 6:08 PM
Mattias Sjögren
>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.
Author
26 Apr 2006 9:26 PM
Willy Denoyette [MVP]
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.

Show quoteHide quote
"Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
news:egXM2wVaGHA.1192@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.
Author
27 Apr 2006 10:26 AM
Sharon
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
Author
27 Apr 2006 11:32 AM
Willy Denoyette [MVP]
"Sharon" <SharonG@newsgroups.nospam> wrote in message
news:DD51CFF1-8C2C-48C8-99DC-510A344F8F7B@microsoft.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.
Author
27 Apr 2006 12:18 PM
Sharon
I think you are right Will. I'll use the PInvoke Win32 API's like "WriteFile"
or "WriteFileEx".


------
Thanks
Sharon
Author
26 Apr 2006 2:40 PM
Stoitcho Goutsev (100)
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)
Show quoteHide quote
"Sharon" <SharonG@newsgroups.nospam> wrote in message
news:4AFA28FC-CF37-43CE-A9D7-5A27240D0E6C@microsoft.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
Author
26 Apr 2006 3:40 PM
Ignacio Machin ( .NET/ C# MVP )
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.

Show quoteHide quote
"Stoitcho Goutsev (100)" <1**@100.com> wrote in message
news:Ogfz66TaGHA.4752@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" <SharonG@newsgroups.nospam> wrote in message
> news:4AFA28FC-CF37-43CE-A9D7-5A27240D0E6C@microsoft.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
>
>

Bookmark and Share