網站首頁 編程語言 正文
實踐過程
效果
代碼
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string flag = null;
PropertyItem[] pi;
string TakePicDateTime;
int SpaceLocation;
string pdt;
string ptm;
Bitmap Pic;
Graphics g;
Thread td;
private void button5_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button1_Click(object sender, EventArgs e)
{
string[] IMG;
listBox1.Items.Clear();
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
IMG = openFileDialog1.FileNames;
if (IMG.Length > 0)
{
for (int i = 0; i < IMG.Length; i++)
{
listBox1.Items.Add(IMG[i]);
}
}
flag = IMG.Length.ToString();
}
}
private void button2_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
flag = null;
}
private void button3_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
txtSavePath.Text = folderBrowserDialog1.SelectedPath;
}
}
private void button4_Click(object sender, EventArgs e)
{
if (flag == null || txtSavePath.Text == "")
{
return;
}
else
{
toolStripProgressBar1.Visible = true;
td = new Thread(new ThreadStart(AddDate));
td.Start();
}
}
private void AddDate()
{
Font normalContentFont = new Font("宋體", 36, FontStyle.Bold);
Color normalContentColor = Color.Red;
int kk = 1;
toolStripProgressBar1.Maximum = listBox1.Items.Count;
toolStripProgressBar1.Minimum = 1;
toolStripStatusLabel1.Text = "開始添加數碼相片拍攝日期";
for (int i = 0; i < listBox1.Items.Count; i++)
{
pi = GetExif(listBox1.Items[i].ToString());
//獲取元數據中的拍照日期時間,以字符串形式保存
TakePicDateTime = GetDateTime(pi);
//分析字符串分別保存拍照日期和時間的標準格式
SpaceLocation = TakePicDateTime.IndexOf(" ");
pdt = TakePicDateTime.Substring(0, SpaceLocation);
pdt = pdt.Replace(":", "-");
ptm = TakePicDateTime.Substring(SpaceLocation + 1, TakePicDateTime.Length - SpaceLocation - 2);
TakePicDateTime = pdt + " " + ptm;
//由列表中的文件創建內存位圖對象
Pic = new Bitmap(listBox1.Items[i].ToString());
//由位圖對象創建Graphics對象的實例
g = Graphics.FromImage(Pic);
//繪制數碼照片的日期/時間
g.DrawString(TakePicDateTime, normalContentFont, new SolidBrush(normalContentColor),
Pic.Width - 700, Pic.Height - 200);
//將添加日期/時間戳后的圖像進行保存
if (txtSavePath.Text.Length == 3)
{
Pic.Save(txtSavePath.Text + Path.GetFileName(listBox1.Items[i].ToString()));
}
else
{
Pic.Save(txtSavePath.Text + "\\" + Path.GetFileName(listBox1.Items[i].ToString()));
}
//釋放內存位圖對象
Pic.Dispose();
toolStripProgressBar1.Value = kk;
if (kk == listBox1.Items.Count)
{
toolStripStatusLabel1.Text = "全部數碼相片拍攝日期添加成功";
toolStripProgressBar1.Visible = false;
flag = null;
listBox1.Items.Clear();
}
kk++;
}
}
#region 獲取數碼相片的拍攝日期
//獲取圖像文件的所有元數據屬性,保存倒PropertyItem數組
public static PropertyItem[] GetExif(string fileName)
{
FileStream Mystream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
//通過指定的數據流來創建Image
Image image = Image.FromStream(Mystream, true, false);
return image.PropertyItems;
}
//遍歷所有元數據,獲取拍照日期/時間
private string GetDateTime(System.Drawing.Imaging.PropertyItem[] parr)
{
Encoding ascii = Encoding.ASCII;
//遍歷圖像文件元數據,檢索所有屬性
foreach (PropertyItem pp in parr)
{
//如果是PropertyTagDateTime,則返回該屬性所對應的值
if (pp.Id == 0x0132)
{
return ascii.GetString(pp.Value);
}
}
//若沒有相關的EXIF信息則返回N/A
return "N/A";
}
#endregion
private void Form1_Load(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (td != null)
{
td.Abort();
}
}
}
原文鏈接:https://zhima.blog.csdn.net/article/details/128102195
相關推薦
- 2022-10-16 Android原生定位服務LocationManager_Android
- 2023-04-08 Linux下動靜態庫的打包與使用指南(C/C++)_C 語言
- 2022-03-17 初學Android之網絡封裝實例_Android
- 2022-04-16 Python數據結構與算法之跳表詳解_python
- 2022-08-26 C++宏函數和內聯函數的使用_C 語言
- 2022-09-28 Python變量定義的簡單使用介紹_python
- 2022-12-12 dos/bat中獲取用戶輸入內容的代碼(保存到文件中)_DOS/BAT
- 2022-05-05 Android開發之自定義加載動畫詳解_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支