c# 獲取照片的經(jīng)緯度和時間的示例代碼
更新時間:2020年11月30日 10:52:34 作者:gisoracle
這篇文章主要介紹了c# 獲取照片的經(jīng)緯度和時間的示例代碼,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
public List<string> GetXYFromPic(String jpgPath)
{
List<string> sXY = new List<string>();
try
{
//載入圖片
Image objImage = Image.FromFile(jpgPath);
//取得所有的屬性(以PropertyId做排序)
var propertyItems = objImage.PropertyItems.OrderBy(x => x.Id);
foreach (PropertyItem objItem in propertyItems)
{
//只取Id范圍為0x0000到0x001e
if (objItem.Id >= 0x0000 && objItem.Id <= 0x001e)
{
switch (objItem.Id)
{
case 0x0002://設(shè)置緯度
if (objItem.Value.Length == 24)
{
//degrees(將byte[0]~byte[3]轉(zhuǎn)成uint, 除以byte[4]~byte[7]轉(zhuǎn)成的uint)
double d = BitConverter.ToUInt32(objItem.Value, 0) * 1.0d / BitConverter.ToUInt32(objItem.Value, 4);
//minutes(將byte[8]~byte[11]轉(zhuǎn)成uint, 除以byte[12]~byte[15]轉(zhuǎn)成的uint)
double m = BitConverter.ToUInt32(objItem.Value, 8) * 1.0d / BitConverter.ToUInt32(objItem.Value, 12);
//seconds(將byte[16]~byte[19]轉(zhuǎn)成uint, 除以byte[20]~byte[23]轉(zhuǎn)成的uint)
double s = BitConverter.ToUInt32(objItem.Value, 16) * 1.0d / BitConverter.ToUInt32(objItem.Value, 20);
double dblGPSLatitude = (((s / 60 + m) / 60) + d);
sXY.Add(dblGPSLatitude.ToString("0.00000000"));
}
break;
case 0x0004: //設(shè)置經(jīng)度
if (objItem.Value.Length == 24)
{
//degrees(將byte[0]~byte[3]轉(zhuǎn)成uint, 除以byte[4]~byte[7]轉(zhuǎn)成的uint)
double d = BitConverter.ToUInt32(objItem.Value, 0) * 1.0d / BitConverter.ToUInt32(objItem.Value, 4);
//minutes(將byte[8]~byte[11]轉(zhuǎn)成uint, 除以byte[12]~byte[15]轉(zhuǎn)成的uint)
double m = BitConverter.ToUInt32(objItem.Value, 8) * 1.0d / BitConverter.ToUInt32(objItem.Value, 12);
//seconds(將byte[16]~byte[19]轉(zhuǎn)成uint, 除以byte[20]~byte[23]轉(zhuǎn)成的uint)
double s = BitConverter.ToUInt32(objItem.Value, 16) * 1.0d / BitConverter.ToUInt32(objItem.Value, 20);
double dblGPSLongitude = (((s / 60 + m) / 60) + d);
sXY.Add(dblGPSLongitude.ToString("0.00000000"));
}
break;
}
}
if (objItem.Id == 0x9003 || objItem.Id == 0x0132)//Id為0x9003表示拍照的時間,0x0132 最后更新時間
{
var propItemValue = objItem.Value;
var dateTimeStr = System.Text.Encoding.ASCII.GetString(propItemValue).Trim('\0');
var dt = DateTime.ParseExact(dateTimeStr, "yyyy:MM:dd HH:mm:ss", CultureInfo.InvariantCulture);
sXY.Add(dt.ToString());//.ToShortDateString()
}
}
objImage.Dispose();
return sXY;
}
catch (Exception ex)
{
//MessageManager.Show(jpgPath + "該圖片文件損壞");
//listErrorMessage.Add(jpgPath + "該照片由于照片損壞,因此無法進行導(dǎo)入。");
return sXY;
}
}
以上就是c# 獲取照片的經(jīng)緯度和時間的示例代碼的詳細內(nèi)容,更多關(guān)于c# 獲取照片的經(jīng)緯度和時間的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#常用數(shù)據(jù)結(jié)構(gòu)棧的詳細介紹
在C#中,Stack<T> 是一個后進先出(LIFO,Last-In-First-Out)集合類,位于System.Collections.Generic 命名空間中,本文詳細介紹C#常用數(shù)據(jù)結(jié)構(gòu)棧,感興趣的朋友跟隨小編一起看看吧2024-09-09
解析C#中的私有構(gòu)造函數(shù)和靜態(tài)構(gòu)造函數(shù)
這篇文章主要介紹了C#中的私有構(gòu)造函數(shù)和靜態(tài)構(gòu)造函數(shù),是C#入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2016-01-01
探秘Unity游戲開發(fā)中的狀態(tài)設(shè)計模式
這篇文章主要介紹了探秘Unity游戲開發(fā)中的狀態(tài)設(shè)計模式,狀態(tài)模式是Unity游戲開發(fā)中常用的一種設(shè)計模式,可以幫助開發(fā)者更好地管理游戲?qū)ο鬆顟B(tài),提高游戲的可維護性和可擴展性2023-05-05

