欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實現(xiàn)給圖片添加日期信息的示例詳解

 更新時間:2022年12月09日 15:44:59   作者:芝麻粒兒  
這篇文章主要為大家詳細介紹了如何利用C#實現(xiàn)給圖片添加日期信息,文中的示例代碼講解詳細,對我們學習C#有一定的幫助,感興趣的小伙伴可以了解一下

實踐過程

效果

代碼

 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 = "開始添加數(shù)碼相片拍攝日期";
            for (int i = 0; i < listBox1.Items.Count; i++)
            {
                pi = GetExif(listBox1.Items[i].ToString());
                //獲取元數(shù)據(jù)中的拍照日期時間,以字符串形式保存
                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;
                //由列表中的文件創(chuàng)建內(nèi)存位圖對象
                Pic = new Bitmap(listBox1.Items[i].ToString());
                //由位圖對象創(chuàng)建Graphics對象的實例
                g = Graphics.FromImage(Pic);
                //繪制數(shù)碼照片的日期/時間
                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()));
                }

                //釋放內(nèi)存位圖對象
                Pic.Dispose();
                toolStripProgressBar1.Value = kk;
                if (kk == listBox1.Items.Count)
                {
                    toolStripStatusLabel1.Text = "全部數(shù)碼相片拍攝日期添加成功";
                    toolStripProgressBar1.Visible = false;
                    flag = null;
                    listBox1.Items.Clear();
                }

                kk++;
            }
        }

        #region 獲取數(shù)碼相片的拍攝日期

        //獲取圖像文件的所有元數(shù)據(jù)屬性,保存倒PropertyItem數(shù)組
        public static PropertyItem[] GetExif(string fileName)
        {
            FileStream Mystream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            //通過指定的數(shù)據(jù)流來創(chuàng)建Image
            Image image = Image.FromStream(Mystream, true, false);
            return image.PropertyItems;
        }

        //遍歷所有元數(shù)據(jù),獲取拍照日期/時間
        private string GetDateTime(System.Drawing.Imaging.PropertyItem[] parr)
        {
            Encoding ascii = Encoding.ASCII;
            //遍歷圖像文件元數(shù)據(jù),檢索所有屬性
            foreach (PropertyItem pp in parr)
            {
                //如果是PropertyTagDateTime,則返回該屬性所對應的值
                if (pp.Id == 0x0132)
                {
                    return ascii.GetString(pp.Value);
                }
            }

            //若沒有相關(guān)的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();
            }
        }
    }

到此這篇關(guān)于C#實現(xiàn)給圖片添加日期信息的示例詳解的文章就介紹到這了,更多相關(guān)C#圖片添加日期信息內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C# String常用函數(shù)的使用詳解

    C# String常用函數(shù)的使用詳解

    這篇文章主要介紹了C# String常用函數(shù)的使用詳解,幫助大家更好的理解和學習使用c#,感興趣的朋友可以了解下
    2021-04-04
  • C#制作多線程處理強化版網(wǎng)絡爬蟲

    C#制作多線程處理強化版網(wǎng)絡爬蟲

    這篇文章主要介紹了C#制作多線程處理強化版網(wǎng)絡爬蟲的相關(guān)代碼,有想學習C#多線程編程的小伙伴可以參考下
    2016-09-09
  • C#?使用PrintDocument類打印標簽的方法

    C#?使用PrintDocument類打印標簽的方法

    本文介紹打印機初步配置,以及實現(xiàn)方法,標簽主要展示資產(chǎn)基本信息以及二維碼,對C#?使用PrintDocument類打印標簽的詳細過程感興趣的朋友一起看看吧
    2022-04-04
  • C#實現(xiàn)排列組合算法完整實例

    C#實現(xiàn)排列組合算法完整實例

    這篇文章主要介紹了C#實現(xiàn)排列組合算法的完整實例,文中實例主要展示了排列循環(huán)方法和排列堆棧方法,需要的朋友可以參考下
    2014-09-09
  • C#中類與結(jié)構(gòu)的區(qū)別實例分析

    C#中類與結(jié)構(gòu)的區(qū)別實例分析

    這篇文章主要介紹了C#中類與結(jié)構(gòu)的區(qū)別,類與結(jié)構(gòu)是C#初學者比較輕易混淆的概念,本文加以實例說明,需要的朋友可以參考下
    2014-08-08
  • C#實現(xiàn)MQTT服務端與客戶端通訊功能

    C#實現(xiàn)MQTT服務端與客戶端通訊功能

    這篇文章介紹了C#實現(xiàn)MQTT服務端與客戶端通訊的功能,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-01-01
  • C#中常見的數(shù)據(jù)緩存方式匯總

    C#中常見的數(shù)據(jù)緩存方式匯總

    在C#開發(fā)中,數(shù)據(jù)緩存是一種優(yōu)化應用程序性能的常見技術(shù),合理的緩存策略可以減少對數(shù)據(jù)源的訪問次數(shù),提高數(shù)據(jù)處理速度,從而改善用戶體驗,下面將詳細介紹幾種在C#中常見的數(shù)據(jù)緩存方式,以及相應的實例,需要的朋友可以參考下
    2024-05-05
  • WinForm 自動完成控件實例代碼簡析

    WinForm 自動完成控件實例代碼簡析

    在Web的應用方面有js的插件實現(xiàn)自動完成(或叫智能提示)功能,但在WinForm窗體應用方面就沒那么好了,接下來參考一下這個實例,看看有沒有以外收獲,感興趣的朋友可以了解下啊,希望本文對你有幫助啊
    2013-01-01
  • 詳解C#中的Async和Await用法

    詳解C#中的Async和Await用法

    這篇文章主要介紹了C#中的Async和Await用法,包括在C#5.0下一些新特性的影響,需要的朋友可以參考下
    2015-07-07
  • Unity UGUI的ContentSizeFitter內(nèi)容尺寸適應器組件使用示例

    Unity UGUI的ContentSizeFitter內(nèi)容尺寸適應器組件使用示例

    這篇文章主要為大家介紹了Unity UGUI的ContentSizeFitter內(nèi)容尺寸適應器組件使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-08-08

最新評論