473,387 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to save InPtr to a raw file?

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
15 8291
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

"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
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
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
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
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
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
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
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
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
>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
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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Maya | last post by:
Hey, there! I'm new to vb.net and it seems I wouldn't be able to solve this without help. I have a pipe delimited file that has to be saved in txt format so it could be accepted by my DTS...
0
by: JTMar | last post by:
I need to save a file in UTF-8 format from an ASP file. Using FileSystemObject I can only save them in Unicode (UTF-16, I think) but not in UTF-8: Set zzzz = fso.OpenTextFile(filepath, 2, true,...
0
by: a | last post by:
Save text file as html kloepper 17:42 23 Jul '04 I'm using httpwebresponse and a StringBuilder to return a stream that originates as a file with the .txt suffix (My download code converts the html...
2
by: Alvo von Cossel I | last post by:
hi, i have a textbox in a form. when you press a Save button, a savefiledialog appears. creating ther file works but everytime you click save, the dialog appears. how do i make the dialog appear...
8
by: DanB | last post by:
This is probably soooo simple but I can't seem to get it. I have a text file that I want users to download via a web page. I want the file to be saved to a default folder (or one that they...
5
by: Patrick | last post by:
Following on from the excellent example at http://www.c-sharpcorner.com/Code/2003/Sept/ExportASPNetDataGridToExcel.asp on how to save a data-grid to excel file, how can I extend the example such...
1
by: Jennyfer Barco | last post by:
A Web Application Hello I have a .NET project, a Web Application and I need to save a file with some data the user selects. If I use the command FileOpen it will save the file in the server and...
4
by: Matt | last post by:
Hi, I would like to save a file locally (on the client computer) in an ASP.net application. It is like the server would return some data that the user can save to a file on his local PC. The ASP...
3
by: jaikant | last post by:
Hi, I am loading a xml file using javascript in Pocket Internet Explorer for local HTML file. I am not able to save the xml file back to the PPC device. foodXML = new...
4
by: Busbait | last post by:
Hi, The below code will allow the user to open the Save As dialog box, Now my question is : after the user specify the file name and the path, how can I modify the below code in order to save...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...

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.