Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 24-Feb-13 13:51pm
Edited 24-Feb-13 14:37pm
Mike Meinz23.2K
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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);
                    }
                }
            }
        }
  Permalink  
v3
Comments
luckycode at 24-Feb-13 19:50pm
   
YA...THIS WORKS FINE...THANK YOU..
SoMad at 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 at 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 at 25-Feb-13 1:29am
   
Ahhh, that is actually a valid point. It probably should work like that.
 
Soren Madsen
luckycode at 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
luckycode at 24-Feb-13 19:52pm
   
I HAVE ONE MORE QUESTION REGARDING THE SAME APPLICATION......I WILL POST IT NOW..
Mike Meinz at 24-Feb-13 19:53pm
   
Updated Solution 1 with code to start Excel and load the DEST.CSV file
luckycode at 1-Jun-13 9:00am
   
sir for the same example above..i want to laod all rgb values in a 2 dimesional array without storing them in excel file or note pad...how do i do this...can u help me..i tried to use lock bits..but i ma getting array...i used lockbits for copying the values to a single dimensional array...so that i can copy all values from single dimensional to two dimensional array
luckycode at 24-Feb-13 20:02pm
   
i have used a small file 70*58...but what about files having 512*512 dimensions...
will it work..or any other alternative
Mike Meinz at 24-Feb-13 20:09pm
   
Excel has a limit on number of rows and columns. With each set of RGB values in a separate row, a large image file will go over that limit. You haven't said why you need these values loaded into Excel and what you are going to do with them, so it is impossible to give good advice.
 
Excel limits for a worksheet: 1,048,576 rows by 16,384 columns
luckycode at 24-Feb-13 20:06pm
   
yes it works..thank you ..
luckycode at 24-Feb-13 20:14pm
   
suppose i have one original picture...and another picture which is the compressed version of the original picture...i want to calculate the difference between each pixel of the original and duplicate(each value of r g b)...thats why i am asking any other alternative...
Mike Meinz at 24-Feb-13 20:20pm
   
You don't say what you want to do with the calculated difference.
 
To compare the RGB values in each pixel of two different images, load each image into a separate Bitmap variable and compare them. Don't bother extracting them out to a file to be loaded into a spreadsheet.
luckycode at 25-Feb-13 2:20am
   
i really have some major work..with the calculated difference...i uploaded one more question..can you c that and help me...

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

  Print Answers RSS
0 DamithSL 300
1 Sergey Alexandrovich Kryukov 287
2 OriginalGriff 195
3 Zoltán Zörgő 149
4 Peter Leow 115
0 OriginalGriff 7,510
1 DamithSL 5,519
2 Sergey Alexandrovich Kryukov 5,044
3 Maciej Los 4,961
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 24 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100