因为家里的照相设备增多,所存的照片也越来越多,如何方便的存储管理起来,这是一个专题。
我想到一个方案:日积月累的照片,全部放到一个目录中,文件名按当时摄影的时间取名,这样可以把照片按时间来区别。这需要读取图片文件中,拍摄时的日期时间数据,它放在照片数据中的EXIF数据区。
我需要一个批量处理程序,把上万张图片的名称按摄影的时间取名。
网上看了看,C# 可以用微软已经做好的功能,直接读取日期时间数据。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Image image = Image.FromFile(@"e:\111.jpg");
PropertyItem[] properties = image.PropertyItems;
foreach (PropertyItem property in properties)
{
switch (property.Id)
{
case 0x0132:
textBox1.Text = ASCIIToString(property.Value);
break;
}
}
}
static string ASCIIToString(byte[] b)
{
return Encoding.ASCII.GetString(b);
}
}
}
---------2021.8.22 批量改名,未完成-------------------------
------------2021.9.6--完成---------------------------
今天终于完成这个小程序,文件名的命名规则:年月日时分秒_文件大小字节.文件后缀。
这样可以最大限度防止文件重名。
小程序功能:对指定目录中的全部图片文件,修改文件名称,名称规则按上面的编码规则。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
List<ZFilesClass> ListFiles = new List<ZFilesClass>();
public Form1()
{
InitializeComponent();
}
static string ASCIIToString(byte[] b)
{
return Encoding.ASCII.GetString(b);
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
String ZPath = null;
System.Windows.Forms.FolderBrowserDialog folder = new System.Windows.Forms.FolderBrowserDialog();
if (folder.ShowDialog() == DialogResult.OK)
{
ZPath = folder.SelectedPath;
}
foreach (string file in Directory.GetFiles(ZPath))
{
ZFilesClass one = new ZFilesClass();
one.FileName = file;
one.ZPath = ZPath;
ListFiles.Add(one);
}
//连接服务器
Control.CheckForIllegalCrossThreadCalls = false;
Thread myconnThread = new Thread(new ThreadStart(SelectFiles));
myconnThread.IsBackground = true;
myconnThread.Start();
}
void SelectFiles()
{
int j = 0;
ListFiles.AsParallel().ForAll(one =>
// Parallel.ForEach (var one in ListFiles)
{
try
{
System.IO.FileInfo fileInfo = new System.IO.FileInfo(one.FileName);
string FileLength = (fileInfo.Length).ToString();
string Ext = fileInfo.Extension.ToString();
Image image = Image.FromFile(one.FileName);
PropertyItem[] properties = image.PropertyItems;
foreach (PropertyItem property in properties)
{
switch (property.Id)
{
case 0x0132:
string dateTimeStr = System.Text.Encoding.ASCII.GetString(property.Value).Trim('\0');
DateTime dt = DateTime.ParseExact(dateTimeStr, "yyyy:MM:dd HH:mm:ss", CultureInfo.InvariantCulture);
string s1 = dt.ToString("yyyyMMdd");
string s2 = dt.ToString("HHmmss");
one.NewFileName = one.ZPath + '\\' + s1 + s2 +'_'+ FileLength + Ext;
break;
}
}
image.Dispose();
listBox1.Items.Add(one.NewFileName + " | " + one.FileName);
fileInfo.MoveTo(one.NewFileName);
toolStripStatusLabel1.Text = "Files:" + (j++).ToString();
}
catch
{
listBox1.Items.Add(one.FileName + "空文件!");
}
});
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
// File.Move(textBox1.Text, textBox2.Text);
}
public class ZFilesClass
{
public String FileName { get; set; }
public String ZPath { get; set; }
public String NewFileName { get; set; }
public String EXIT_Time { get; set; }
public int RepeatID { get; set; }
}
}
}