简体   繁体   English

转换图像并另存为字节数组

[英]convert image and save as byte array

I would like to convert simple picture to byte array and then save this array to .txt I tried to do this but I'm lost.. I can't convert this ... I would like to have array and at the place where are figures in my array should appear 1. If there is nothing should be 0. 我想将简单的图片转换为字节数组,然后将该数组保存为.txt,我试图这样做,但我迷路了。是我数组中的数字应显示为1。如果没有,则应为0。

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Image image = Image.FromFile(@"D:\Stackimage\1.png");
            var ms = new MemoryStream();

            image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

            var bytes = ms.ToArray();



            string[] tab = new string[1000000];

            for (int i = 0; i < 1000000; i++)
            {

                tab[i] = "" + bytes;
            }
            MessageBox.Show("start");
            using (System.IO.StreamWriter writer = new System.IO.StreamWriter(@"D:\data.txt"))
            {
                foreach (string elem in tab)
                {
                    writer.WriteLine(elem);
                }
            }
            MessageBox.Show("stop");
        }

I add here my image. 我在这里添加我的图片。 My picture 我的照片

I see what you are trying to do. 我知道你在做什么。 A similar approach is used in terrain rendering with an heightMap. 在带有heightMap的地形渲染中使用了类似的方法。 The idea is to map an image as grey scale into a text file, which in turn is used to generate the height of a terrain. 想法是将灰度图像映射到文本文件中,该文本文件又用于生成地形的高度。

In your case you can just map it as 0s and 1s(where 1s is the colour of your obstacles). 在您的情况下,您可以将其映射为0和1(其中1s是障碍物的颜色)。

You can then upload the mapped text file into a 2D array which will be your map bird-eye view. 然后,您可以将映射的文本文件上传到2D数组中,这将是您的地图鸟瞰图。

As the bot moves across the scene you can detect whether it can safely move or not by checking the current position on the 2D array map. 当机器人在场景中移动时,您可以通过检查2D数组地图上的当前位置来检测它是否可以安全移动。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM