Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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);
            }
        }
    }
}
 
in the above program..when i select a file..excel sheet is created with a name call "dest.csv"..and i GET pixel data for 1 single file...till here it is fine...i select the file using open file dialog...but if i have set of pictures and open them using folder browse dialog..i want an excel sheet to be created for each file in the folder....
how to automatically rename the dest.CSV for each file..
Posted 24-Feb-13 14:16pm
Edited 24-Feb-13 14:38pm
SoMad116.1K
v3

1 solution

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

Solution 1

As I said in my last comment to your prior question, I don't think you should be creating files to be used in Excel. I think you should load the two images into two Bitmap variables and do your comparison within the program.
 
That said, below is a sample program that does what you requested.
 
_______________________________________________________________________________________
 
1. Selects .JPG files from the directory in textBox1.Text
    Example: C:\\Users\\Mike\\Pictures\\Family
2. Uses original filename with .CSV replacing .JPG
3. Displays WaitCursor during operation
4. Counts files created and displays result when done
5. Uses Application.DoEvents() to allow other Windows processes to run occasionally
 

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

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            int fileCount = 0;
            Cursor.Current = Cursors.WaitCursor;
            IEnumerable<string> files = Directory.EnumerateFiles(textBox1.Text);  // textBox1.Text contains a directory name
            foreach (var sourceFilename in files)
            {
                // Construct new filename substituting .CSV for the file extension
                string newFilename = System.IO.Path.GetDirectoryName(sourceFilename) + "\\" + System.IO.Path.GetFileNameWithoutExtension(sourceFilename) + ".CSV"; // Use filename and append .CSV extension
                string fileExtension = System.IO.Path.GetExtension(sourceFilename).ToLower(); // Get the original file extension
                if (fileExtension == ".jpg")  // Select only jpg files 
                {
                    using (FileStream fs = File.Create(newFilename)) // Create the new CSV file
                    {
                        using (TextWriter w = new StreamWriter(fs))
                        {
                            int i;
                            int j;
                            Bitmap bm = new Bitmap(sourceFilename);
                            for (i = 0; i < bm.Width; i++)
                            {
                                Application.DoEvents();  // Let other Windows processes run
                                for (j = 0; j < bm.Height; j++)
                                {
                                    Application.DoEvents();  // Let other Windows processes run
                                    Color pixelColor = bm.GetPixel(i, j);
                                    w.Write(pixelColor.R + "," + pixelColor.G + "," + pixelColor.B + "\r\n");
                                }
                            }
                            w.Close(); // Close the CSV file
                            fileCount++;
                        }
 
                    }
                }
                Application.DoEvents(); // Let other Windows processes run
            }
            Cursor.Current = Cursors.Default;
            MessageBox.Show(String.Format("{0} files processed.", fileCount));
        }
 
    }
}
  Permalink  
v3
Comments
luckycode at 25-Feb-13 22:53pm
   
I saw ur code just now and understood it...but i dont have System.Linq..i dowloaded core.dll and placed in visualstudio/vs/../x86/..and when i wanted to add reference via form by using add refrence...i dont find system.linq..or system.core...i am trying to fix the problem...how to add reference of system.linq
luckycode at 25-Feb-13 23:52pm
   
I took a listbox and added all files in that list box instead of using IENUMERABLE...I DONT HAVE System.linq...mine is visual studio 2005....netframework2...so it is difficult to get LINQ...ANY WAYS MY PROBLEM IS SOLVED...THANKS FOR THE CODE..ONCE AGAIN...
Mike Meinz at 26-Feb-13 8:01am
   
Sorry. System.Linq is not required. It was left in my test program from an old test. I removed it from the Solution.

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



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