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);
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();
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);
}
}
}
}