Click here to Skip to main content
15,884,837 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am reading rgb values of each pixel in an application and storing it in an excel sheet.
when i select the file and run the application/..i get a message..that application is running at the bottom of screen...but actual excel sheet is filled with data..and even if i refresh it...it is not getting loaded(i mean the size does not change..)...the code is given below...plz chk
///some code
  using (FileStream fs = File.Create("DEST.XLS"))
                    {
                        using (TextWriter w = new StreamWriter(fs))
                        {
                            Bitmap bm = new Bitmap(textBox1.Text);
//textbox siginifes the file we are selcting for reading pixel data
                            for (i = 0; i < bm.Width; i++)
                            {
                                for (j = 0; j < bm.Height; j++)
                                {
                                    Color pixelColor = bm.GetPixel(i, j);
                                    b = pixelColor.R;
                                    w.Write(b + " ");
                                    c = pixelColor.G;
                                    w.Write(c + " ");
                                    d = pixelColor.B;
                                    w.Write(d + " ");
                                    w.Write("\r\n");
                                }
                            }
                            w.Close();
                        }
                    }


//DATA.XLS...GETS CREATED FROM WHERE WE SELECTE THE IMAGE.

//IS THE FORMAT WHICH I HAVE WRITTEN FOR EXCEL FILE CORRECT..BECAUSE A MSG COMES..IT IS NOT IN CORRECT FORMAT...BUT I AM ABLE TO OPEN IT
Posted
Updated 24-Feb-13 13:37pm
v3

1 solution

I made a few changes to your code and tested the following code. It creates a comma-separated values (CSV) file that will be loaded into Excel when you double-click the DEST.CSV filename in File Explorer. It works fine for smaller image files. For larger image files, it creates a file that is too big (too many rows) for Excel to load.

Changes I made:
1. Renamed file to DEST.CSV
2. Put commas between R and G values and between G and B values
3. Declared all variables


using System.Diagnostics;

...

        private void button1_Click(object sender, EventArgs e)
        {
            using (FileStream fs = File.Create("DEST.CSV"))
            {
                using (TextWriter w = new StreamWriter(fs))
                {
                    int i;
                    int j;
                    int b;
                    int c;
                    int d
                    Bitmap bm = new Bitmap(textBox1.Text);
                    //textbox signifies the file we are selecting for reading pixel data
                    for (i = 0; i < bm.Width; i++)
                    {
                        for (j = 0; j < bm.Height; j++)
                        {
                            Color pixelColor = bm.GetPixel(i, j);
                            b = pixelColor.R;
                            w.Write(b + ",");
                            c = pixelColor.G;
                            w.Write(c + ",");
                            d = pixelColor.B;
                            w.Write(d);
                            w.Write("\r\n");
                            
                        }
                    }
                    w.Close();
                    //
                    // Load DEST.CSV into Excel
                    Process myProcess = new Process();
                    try
                    {
                        myProcess.StartInfo.UseShellExecute = true;
                        myProcess.StartInfo.FileName = "DEST.CSV";
                        myProcess.StartInfo.CreateNoWindow = true;
                        myProcess.Start();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }
 
Share this answer
 
v3
Comments
luckycode 24-Feb-13 19:50pm    
YA...THIS WORKS FINE...THANK YOU..
SoMad 24-Feb-13 20:45pm    
I see you accepted this as an answer. You should also vote 5 on it, especially since Mike continues to help you out here.

Soren Madsen
luckycode 25-Feb-13 1:26am    
i thought once u accept a an answer...the votes will be added automatically...now i ahve seen the voting system
SoMad 25-Feb-13 1:29am    
Ahhh, that is actually a valid point. It probably should work like that.

Soren Madsen
luckycode 25-Feb-13 1:35am    
ok...atleast now you r convinced...thank you..for ur comment...I learned that we have to vote separately...even if u accept a soloution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900