lina lina

lina lina

  • NA
  • 7
  • 5.6k

Problem to save array into text file

Nov 23 2011 11:24 PM
Hi Everyone,


I have the problem when I tried to save my array into text file... Below is my code with the problem (bold mark):
for the reply, I very appreciate.. thanks


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace array2d
{
      public partial class Form1 : Form
        {
            private System.Windows.Forms.Button button1 = new Button();
            private System.Windows.Forms.PictureBox pictureBox1 = new PictureBox();
            private Random _rnd = new Random();

            public Form1()
            {
                InitializeComponent();

                //this is usually done in the designer
                this.button1.Location = new System.Drawing.Point(180, 47);
                this.button1.Name = "START";
                this.button1.Size = new System.Drawing.Size(75, 23);
                this.button1.TabIndex = 0;
                this.button1.Text = "START";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click);

                this.pictureBox1.Location = new System.Drawing.Point(13, 115);
                this.pictureBox1.Name = "pictureBox1";
                this.pictureBox1.Size = new System.Drawing.Size(128, 256);
                this.pictureBox1.TabIndex = 1;

                this.ClientSize = new System.Drawing.Size(500, 470);
                this.Controls.Add(this.pictureBox1);
                this.Controls.Add(this.button1);
            }

            private void button1_Click(object sender, EventArgs e)
            {
                //do a fake image processing for a 32bpp bitmap
                //note that I use 4 bytes for one pixel, 
                //each 4th byte represents the alpha-channel

                //set up an array (x must be multiple of 4)
                byte[,] myMatrix = new byte[800, 200];

                //fill with random (color-) values
                for (int y = 0; y < myMatrix.GetLength(1); y++)
                {
                    for (int x = 0; x < myMatrix.GetLength(0); x++)
                    {
                        myMatrix[x, y] = (byte)_rnd.Next(256);
            
                        


                    }
                }

                //now we have to convert the 2 dimensional array into a one dimensional byte-array
                byte[] pixels = new byte[myMatrix.GetLength(0) * myMatrix.GetLength(1)];
                for (int y = 0; y < myMatrix.GetLength(1); y++)
                {
                    for (int x = 0; x < myMatrix.GetLength(0); x++)
                    {
                        pixels[y * myMatrix.GetLength(0) + x] = myMatrix[x, y];
                       string file_name = "C:\\test.txt";
                        string tLine = "";
                        System.IO.StreamWriter objWriter;
                        objWriter = new System.IO.StreamWriter(file_name);
                        // string[] aryTest = new string[32768];
                        for (int i = 0; i < 5; i++)
                        {
                            tLine = tLine + pixels[i] + "\r\n";
                            objWriter.WriteLine(pixels[i]);
                        }
                       
                    }
                }


                
            }


        }
    }


Answers (3)