gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 10:29:16 25 4
gpt4 key购买 nike

我在 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();

附上我的完整 code

最佳答案

我想提一下这里的代码和你链接中的代码是不同的......

无论如何,按照您链接中的那个:

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();

您实际上不需要将深度数据转换为字符串。 BinaryWriter 实际上可以取 ushort value在其重载之一。为什么不只是遍历并写出来呢?另外,您应该使用 using statements为您的文件流和二进制编写器。

尝试以下操作:

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);
}
}
}

关于c# - 将 ushort 图像转换为二进制图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30778477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com