如何计算更多文件的数据......一次尝试.. [英] how to calculate data for more files..in a single attempt..

查看:61
本文介绍了如何计算更多文件的数据......一次尝试..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





以上程序..当我选择一个文件.excel表创建时使用名称调用dest.csv..并且我获取1个单个文件的像素数据...直到这里它很好...我使用打开文件对话框选择文件。 ..但如果我有一组图片并使用文件夹浏览对话框打开它们..我想为文件夹中的每个文件创建一个Excel工作表....

如何自动重命名dest .CSV用于每个文件..



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..

推荐答案

正如我在上一次对你之前的问题的评论中所说,我认为你不应该创建要使用的文件在Excel中。我认为您应该将两个图像加载到两个Bitmap变量中并在程序中进行比较。



也就是说,下面是一个示例程序,可以满足您的要求。



_______________________________________________________________________________________


1.从textBox1.Text中的目录中选择.JPG文件

    示例: C:\\Users \\ Mike \\Pictures \\ FamilyJamily

2.使用原始文件名 CSV替换 JPG

3.在操作期间显示WaitCursor

4.计算创建的文件并在完成后显示结果

5.使用Application.DoEvents()允许其他Windows进程偶尔运行







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

    }
}


这篇关于如何计算更多文件的数据......一次尝试..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆