如何保存字节和指针中的原始图像文件? [英] how can I save a raw image files from bytes and pointers?

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

问题描述

如何保存字节和指针中的原始图像文件?
我的代码在下面....


int值= Marshal.ReadByte(pMem);

int大小=宽度*高度* bpp/8;

byte [] abr =新的byte [size];


元帅.Copy(pMem,abr,0,size);



MemoryStream ms =新的MemoryStream(abr);
图片img = Image.FromStream(ms);
img.Save(@"c:\ data.raw",img.RawFormat);

但是它不起作用 :( :( :(:(

How can I save a raw image files from bytes and pointers?
my codes are below....


int value = Marshal.ReadByte(pMem);

int size = width*height*bpp/8;

byte[] abr = new byte[size];


Marshal.Copy(pMem, abr, 0, size);



MemoryStream ms = new MemoryStream(abr);
Image img = Image.FromStream(ms);
img.Save(@"c:\data.raw", img.RawFormat);

but it doesnt work :( :( :(

推荐答案

Marshal.复制在另一个方向上起作用(从托管数组复制到非托管数组).

如果pMem是指向非托管字节数组的指针,并且size是数组的大小
然后
Marshal.Copy works in the other direction (copies from managed array TO unmanaged array).

if pMem is an pointer to the unmanaged byte array, and size is the size of the array
then
byte[] abr=new byte[size];
for(int i=0; i<size; i++)
  abr[i]=Marshal.ReadByte(pMem,i);



会将字节从非托管复制到托管.
您需要将代码标记为不安全,然后进行编译以允许使用不安全的代码.



will copy the bytes FROM the unmanaged to managed.
You will need to mark the code as unsafe, and compile allowing unsafe code.


我看到
这行有两个问题
I see two problems with the line
img.Save(@"c:\data.raw", img.RawFormat);



1.保存到驱动器的根目录通常会导致安全错误.
2. img.Rawformat可能根本不起作用(您是否检查过它是否实际上返回了null以外的其他东西?),因为在读取字节数组时原始文件格式信息可能会丢失.相反,您应该指定想要写入文件的格式,例如ImageFormat.Png,并为输出文件提供适当的扩展名.请注意,没有ImageFormat.Raw,也没有公认的文件类型".raw".

如果您实际上说出它不起作用"是有帮助的,那么您会得到例外吗?如果是这样,错误消息是什么?尝试将img.Save()方法调用放在try-catch块中,然后调试异常.



1. saving to the root of a drive often causes security errors.
2. img.Rawformat might not work at all (have you checked to see if this is actually returning anything other than null?), since it''s possible that the original file format info is lost in reading the byte array. Instead, you should specify the format you want the file written in, such as ImageFormat.Png, and give the output file the appropriate extension. Note that there is no ImageFormat.Raw, nor a recognized file type ".raw".

It would be helpful if you actually said what you mean by "it doesn''t work": do you get an exception? if so, what is the error message? Try putting the img.Save() method call inside a try-catch block, and debug the exception.


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

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