-2

画像 (.jpg) の名前を変更する必要があり、新しい名前には撮影日を含める必要があります。画像の撮影日を取得できますが、新しいファイル名に含めることはできません。

Image im = new Bitmap("FileName.....");
PropertyItem pi = im.GetPropertyItem(0x132);
dateTaken = Encoding.UTF8.GetString(pi.Value);
dateTaken = dateTaken.Replace(":", "").Replace(" ", "");
string newName = dateTaken +".jpg" ;
MessageBox.Show(newName.ToString()); 
4

2 に答える 2

0

メッセージボックスに表示しようとしている文字列に日付を取得できないという問題ですか、それとも画像のファイル名を変更しようとしていますか? イメージ ファイル名を変更する場合は、ファイル自体を変更する必要があります。C#でファイル名の一部を置き換えるを見てください

于 2013-06-03T07:12:32.757 に答える
-1

jpeg ファイルの名前を変更したい場合は、以下のコードを試すことができます。

このコードは、画像から日付を抽出し (画像の完全なファイル パスが必要です)、別の形式に変換してから、それを新しいファイル名として使用します。ファイルの名前を変更するコードはコメント アウトされているため、ローカル マシンで試す前にコンソールで結果を確認できます。

サンプルコード。独自の完全修飾ファイル パスを使用してください

using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;

// This is just an example directory, please use your fully qualified file path
string oldFilePath = @"C:\Users\User\Desktop\image.JPG";
// Get the path of the file, and append a trailing backslash
string directory = System.IO.Path.GetDirectoryName(oldFilePath) + @"\";

// Get the date property from the image
Bitmap image = new Bitmap(oldFilePath);
PropertyItem test = image.GetPropertyItem(0x132);

// Extract the date property as a string
System.Text.ASCIIEncoding a = new ASCIIEncoding();
string date = a.GetString(test.Value, 0, test.Len - 1);

// Create a DateTime object with our extracted date so that we can format it how we wish
System.Globalization.CultureInfo provider = CultureInfo.InvariantCulture;
DateTime dateCreated = DateTime.ParseExact(date, "yyyy:MM:d H:m:s", provider);

// Create our own file friendly format of daydayMonthMonthYearYearYearYear
string fileName = dateCreated.ToString("ddMMyyyy");

// Create the new file path
string newPath = directory + fileName + ".JPG";

// Use this method to rename the file
//System.IO.File.Move(oldFilePath, newPath);

Console.WriteLine(newPath);
于 2013-06-03T07:58:08.270 に答える