Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将ushort图像转换为二进制?_C#_Image - Fatal编程技术网

C# 将ushort图像转换为二进制?

C# 将ushort图像转换为二进制?,c#,image,C#,Image,我在ushort变量中有一个图像,希望以二进制格式保存此图像 任何人,请告诉我如何使用C# 我试过这个,但不起作用 ushort[] Depthdata; Depthdata = new ushort[DWidth * DHeight]; string s1 = string.Format("{0}", count_depth); FileStream fs = new FileStream("C:\\img" + s1 + ".bin", FileMode.Create, FileAccess

我在ushort变量中有一个图像,希望以二进制格式保存此图像

任何人,请告诉我如何使用C#

我试过这个,但不起作用

ushort[] Depthdata;
Depthdata = new ushort[DWidth * DHeight];
string s1 = string.Format("{0}", count_depth);
FileStream fs = new FileStream("C:\\img" + s1 + ".bin", FileMode.Create, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs);
string image_str = Convert.ToString(Imagedata);
bw.Write(image_str);
bw.Close();
fs.Close();

这里是我的完整附件

我想说的是,这里的代码与您链接中的代码不同

在任何情况下,通过链接中的链接:

ushort[] Depthdata;
....
string s1 = string.Format("{0}", count_depth);
FileStream fs = new FileStream("G:\\test" + s1 + ".bin", FileMode.Create, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs);
string depth_str = Convert.ToString(Depthdata);
bw.Write(depth_str);
bw.Close();
fs.Close();
实际上,您不需要将Depthdata转换为字符串。BinaryWriter实际上可以接受其重载之一。为什么不反复阅读并写出它呢?此外,您还应该使用filestream和binarywriter

请尝试以下操作:

using(FileStream fs = new FileStream("G:\\test" + s1 + ".bin", FileMode.Create, FileAccess.Write))
{
    using(BinaryWriter bw = new BinaryWriter(fs))
    {
        foreach(ushort value in Depthdata)
        {
            bw.write(value);
        }
    }
}

我想这会对你有帮助。我在*.tiff文件上测试了这个

首先制作单独的类Ext

public static class Ext
    {

        public static string ToHexString(this byte[] hex)
        {
            if (hex == null) return null;
            if (hex.Length == 0) return string.Empty;

            var s = new StringBuilder();
            foreach (byte b in hex)
            {
                s.Append(b.ToString("x2"));
            }
            return s.ToString().ToUpper();
        }
    }
然后,您可以添加以下代码将图像转换为字符串二进制

 FileStream fs=new FileStream(ImgPathID, FileMode.Open, FileAccess.Read); //set file stream
 Byte[] bindata=new byte[Convert.ToInt32(fs.Length)];
 fs.Read(bindata, 0, Convert.ToInt32(fs.Length));
string  bindatastring = Ext.ToHexString(bindata);// call to class

对于给予-1,请解释原因,以便我可以更新或检查我有什么问题doing@Addee,如果您的图像可以放入
ushort
,则图像必须非常小。它可能是一个
ushort
数组吗?@FrédéricHamidi:Depthdata的定义如下
ushort[]Depthdata
Depthdata=new-ushort[DWidth*DHeight]IplImage
而不是
ushort
IplImage
有一个名为
ToBitmap()
的方法,只需使用它,然后使用.Save(文件名)。如果你用的是我写的东西,你就不需要用了……你能告诉我这方面的extact代码吗?我能把
IplImage
也保存到.bin吗?这里有一个很好的解释