C#下取得Exif中照片拍攝日期

meil發表於2015-10-08
        /// <summary>
        /// 獲取Exif中的照片拍攝日期
        /// </summary>
        /// <param name="fileName">檔名</param>
        /// <returns>拍攝日期</returns>
        private string GetTakePicDate(string fileName)
        {
            Encoding ascii = Encoding.ASCII;
            string picDate;

            FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            Image image = Image.FromStream(stream, true, false);

            foreach (PropertyItem p in image.PropertyItems)
            {
                //獲取拍攝日期時間
                if (p.Id == 0x9003) // 0x0132 最後更新時間
                {
                    stream.Close();

                    picDate =  ascii.GetString(p.Value);
                    if ((!"".Equals(picDate)) && picDate.Length >= 10)
                    {
                        // 拍攝日期
                        picDate = picDate.Substring(0, 10);
                        picDate = picDate.Replace(":","-");
                        return picDate;
                    }
                }
            }
            stream.Close();
            return "";
        }

 

相關文章