0

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.

 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

Saurabh Solanki
  • 2,146
  • 18
  • 31
Kim
  • 25
  • 3
  • Possible duplicate of [Reliable way to convert a file to a byte\[\]](https://stackoverflow.com/questions/1497997/reliable-way-to-convert-a-file-to-a-byte) – Liam Nov 13 '18 at 10:01
  • 1
    Why do you have a loop where you set `"" + bytes`? Shouldn't you convert the bytes list to a string list instead? – Haytam Nov 13 '18 at 10:02
  • 1
    What your doing doesn't really make a lot of sense. Why would you want this? You'll just end up with an unreadable text file. – Liam Nov 13 '18 at 10:02
  • Are you trying to encode the bytes into a a readable format of some kind? What format do you want? – Liam Nov 13 '18 at 10:05
  • I am going to program a small mobile robot with collision detection. I have 512 lines (it will be a robot sensor) and 2 figure in different places (obstacles). I have 1000 pictures with different figure positions. And then I have to check where the figure are (where they are cutting with the line). Generally, I need to know where the line obstacle is and where the circle obstacle is, and I need this data because then I will teach the neural network. – Kim Nov 13 '18 at 10:16

1 Answers1

0

I see what you are trying to do. A similar approach is used in terrain rendering with an 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).

You can then upload the mapped text file into a 2D array which will be your map bird-eye view.

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.

Hope it helps.

Alex Leo
  • 2,781
  • 2
  • 13
  • 29