本文介绍了如何计算更多文件的数据......一次尝试..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 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..推荐答案 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)); } }} 这篇关于如何计算更多文件的数据......一次尝试..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-02 16:45