如何将InPtr保存到原始文件? [英] How to save InPtr to a raw file?

查看:93
本文介绍了如何将InPtr保存到原始文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好大师,


我有一个System.IntPtr指向我图像的内存地址(不是

..NET图像)。 br />
我希望将IntPtr指向的图像数据保存到原始数据文件中。我现在的方式是:
$

System.IO.FileStream file = new System.IO.FileStream(fileName,

System.IO.FileMode.Create);

file.Write(byte [],0,...);

file.Close();


但是FileStream.Write占用了我没有的byte []。

如何将IntPtr指向的图像数据保存到文件中?


---------

谢谢

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

推荐答案

你的IntPtr是位图句柄(hBitmap)还是指向原始数据的直接指针?
如果是,你可以构造一个Image.FromHBitmap然后序列化

图像类。

否则你需要将它转换为byte [],例如通过

System.Runtime.InteropServices.Marshal.ReadIntPtr或喜欢。


劳拉


" Sharon" <嘘***** @ newsgroups.nospam> ha scritto nel messaggio

新闻:4A ********************************** @ microsof t.com ...
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,

我有一个System.IntPtr指向我图像的内存地址(不是.NET图像我希望将IntPtr指向的图像数据保存到原始数据文件中。
我现在的方法是:

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

但FileStream.Write采用了我没有的byte []。
如何将IntPtr指向的图像数据保存到文件中?

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





" ;沙龙" <嘘***** @ newsgroups.nospam>在消息中写道

新闻:4A ********************************** @ microsof t.com ...

|你好大师,

|

|我有一个System.IntPtr指向我的图像的内存地址(而不是

| .NET图像)。

|我希望将IntPtr指向的图像数据保存到原始数据文件中。



|我现在的方式是:

|

| System.IO.FileStream file = new System.IO.FileStream(fileName,

| System.IO.FileMode.Create);

| file.Write(byte [],0,...);

| file.Close();

|

|但是FileStream.Write占用了我没有的byte []。

|如何将IntPtr指向的图像数据保存到文件中?

|

|

|

| ---------

|谢谢

| Sharon


您可以使用Marshal.Copy将内存内容复制到一个字节[]。


Willy。

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


Sharon,


您使用IntPtr这一事实让我相信您的数据在

非托管内存中。要获取托管字节数组中的数据,您可以使用

System.Runtime.InteropServices.Marshal类。

您可以使用Marshal.ReadByte方法循环访问数据

非托管内存并在托管字节[]中一次复制一个字节,或者你可以创建一个包含字节数组的结构并使用Marshal.PtrToStructure

立即读取它的方法。后者我没试过,但应该工作。

-

HTH

Stoitcho Goutsev(100)

Sharon <嘘***** @ newsgroups.nospam>在消息中写道

新闻:4A ********************************** @ microsof t.com ...
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大师,

我有一个System.IntPtr指向我图像的内存地址(不是.NET图像) 。
我希望将IntPtr指向的图像数据保存到原始数据文件中。
我现在的方法是:

System.IO。 FileStream file = new System.IO.FileStream(fileName,
System.IO.FileMode.Create);
file.Write(byte [],0,...);
文件。关闭();

但FileStream.Write采用我没有的byte []。
如何将IntPtr指向的图像数据保存到文件中?

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



这篇关于如何将InPtr保存到原始文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆