|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How to save InPtr to a raw file?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 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
Show quote
Hide quote
"Sharon" <SharonG@newsgroups.nospam> wrote in message You can use Marshal.Copy to copy the memory contents to a byte[].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 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. -- Show quoteHide quoteStoitcho 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. > > 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. | > | > | | Yup, you are right. My apologies.
-- Show quoteHide quoteStoitcho 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. > | > > | > > | > | > > 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. >> | > >> | > >> | >> | >> >> > > Hi,
You are correct, Copy is to send data to unmanaged -- Show quoteHide quoteIgnacio 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. >> >> > > 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. | >> | >> | > | > | | >Still using V1.0? Marshal.Copy has overloads to copy from unmanaged to Since 1.0 even.>managed since v1.1. 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 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. 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 "Sharon" <SharonG@newsgroups.nospam> wrote in message Which is using unmanaged memory for the image data, it's simply not possible news:DD51CFF1-8C2C-48C8-99DC-510A344F8F7B@microsoft.com... | I'm not using unmanaged memory, I'm simply using a 3'rd party library otherwise. | (Atalasoft DotImage) that has a AtalaImage object, and this object hold an You can't avoid the copy, unless you resort to unmanaged API's for writing.| 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. | It does not, no single graphics package can hold it's raw image data in the | Assuming the AtalaImage object holds it data in a managed memory, 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 In this case you'll need to PInvoke Win32 API's "WriteFile" or 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. "WriteFileEx". Willy. I think you are right Will. I'll use the PInvoke Win32 API's like "WriteFile"
or "WriteFileEx". ------ Thanks Sharon 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. -- Show quoteHide quoteHTH 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 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 > >
Running a process in a thread?
Set primarykey to a datatable Copy file from http location redefinition of generic methods C# Extended Text Editor TreeView populate on demand and mutations after expanding Serialization Change RowState property of DataRow null value How to access main form functions from child forms |
|||||||||||||||||||||||