1

私は現在、C# を使用して Windows フォーム アプリケーションに取り組んでいます。画像が作成された日時を取得する方法を知りたいです。オンラインで解決策を確認したところ、PropertyItem への参照が見つかりましたが、その使用方法がわかりません。

以下のコードは、フォルダーから写真を取得し、それらを画像配列に表示します。
画像がクリックされたときにメッセージボックスに画像が作成された日付と時刻を表示するにはどうすればよいですか?

    // Function to add PictureBox Controls
    private void AddControls(int cNumber)
    {
        imgArray = new System.Windows.Forms.PictureBox[cNumber]; // assign number array 
        for (int i = 0; i < cNumber; i++)
        {
            imgArray[i] = new System.Windows.Forms.PictureBox(); // Initialize one variable
        }
        // When call this function you determine number of controls
    }

    private void ClickImage(Object sender, System.EventArgs e)
    {
        // On Click: load (ImageToShow) with (Tag) of the image
        ImageToShow = ((System.Windows.Forms.PictureBox)sender).Tag.ToString();
        // then view this image on the form (frmView)

        PrivacyDefenderTabControl.SelectedIndex = 5;
        LogsPhotosPictureBox.Image = Image.FromFile(ImageToShow);
        LogsPhotosPictureBox.Left = (this.Width - LogsPhotosPictureBox.Width) / 15;
    }

    private void ImagesInFolder()
    {
        FileInfo FInfo;
        // Fill the array (imgName) with all images in any folder 
        imgName = Directory.GetFiles(Application.StartupPath + @"\Faces");
        // How many Picture files in this folder
        NumOfFiles = imgName.Length;
        imgExtension = new string[NumOfFiles];
        for (int i = 0; i < NumOfFiles; i++)
        {
            FInfo = new FileInfo(imgName[i]);
            imgExtension[i] = FInfo.Extension; // We need to know the Extension
        }
    }

    private void ShowFolderImages()
    {
        int Xpos = 27;
        int Ypos = 8;
        Image img;
        Image.GetThumbnailImageAbort myCallback =
            new Image.GetThumbnailImageAbort(ThumbnailCallback);
        MyProgress.Visible = true;
        MyProgress.Minimum = 0;
        MyProgress.Maximum = NumOfFiles;
        MyProgress.Value = 0;
        MyProgress.Step = 1;
        string[] Ext = new string[] { ".GIF", ".JPG", ".BMP", ".PNG" };
        AddControls(NumOfFiles);
        for (int i = 0; i < NumOfFiles; i++)
        {
            switch (imgExtension[i].ToUpper())
            {
                case ".JPG":
                case ".BMP":
                case ".GIF":
                case ".PNG":
                    img = Image.FromFile(imgName[i]); // or img = new Bitmap(imgName[i]);
                    imgArray[i].Image = img.GetThumbnailImage(64, 64, myCallback, IntPtr.Zero);
                    img = null;
                    if (Xpos > 360) // six images in a line
                    {
                        Xpos = 27; // leave eight pixels at Left 
                        Ypos = Ypos + 72;  // height of image + 8
                    }
                    imgArray[i].Left = Xpos;
                    imgArray[i].Top = Ypos;
                    imgArray[i].Width = 64;
                    imgArray[i].Height = 64;
                    imgArray[i].Visible = true;
                    // Fill the (Tag) with name and full path of image
                    imgArray[i].Tag = imgName[i];
                    imgArray[i].Click += new System.EventHandler(ClickImage);
                    this.LogsTabPage.Controls.Add(imgArray[i]);
                    Xpos = Xpos + 72; // width of image + 8
                    Application.DoEvents();
                    MyProgress.PerformStep();
                    break;
            }
        }
        MyProgress.Visible = false;
    }
4

2 に答える 2

2

は、ロードされるImageと、元のファイルとは何の関係もありません。ユーザーがクリックしたときに、画像の元のファイル名を取得できる場合は、次を使用できます。

var lastEditDate = File.GetLastWriteTime("file.jpg");

または、すでにFileInfoインスタンスを使用している場合は、次を使用できます。

var lastEditDate = new FileInfo("file.jpg").LastWriteTime;

から派生させることはできないためImage、そのコンストラクターは内部であるため、Dictionary<Image,string>どこかに を保持し、画像が読み込まれるときにそれを入力してから、ルックアップを通じてクリックされた画像の相対パスを取得できます。

于 2013-01-02T05:39:24.653 に答える
0

少なくとも jpeg ファイルにはメタデータが添付されています。注意: 添付されている可能性がありますが、そこにあることを保証することはできません。

既に画像 (Image.FromFile) を読み込んでおり、結果のオブジェクトはメソッドGetPropertyItemをサポートしています。そのプロパティを使用すると、メタデータを読み取ることができます。利用可能なプロパティのリスト(0x0132 は日付と時刻) とサンプルがあります。

于 2013-01-02T05:40:36.323 に答える