C# 将ushort图像转换为二进制?
我在ushort变量中有一个图像,希望以二进制格式保存此图像 任何人,请告诉我如何使用C# 我试过这个,但不起作用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[] 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吗?这里有一个很好的解释