给照片添加水印

今年过年出门去玩,回家想挑选照片冲洗出来收进相册,但是时间久了又怕忘记什么时候拍摄,在哪里拍摄的照片,遂动手写个小程序,给照片添加上时间 地点的水印。文章末尾附上 程序传送门

主要思路:

1.读取照片的Exif信息,从中获取照片的拍摄时间、经纬度(度分秒)、照片宽度、高度等信息;

用到 MetadataExtractor 库

 

  var rmd = ImageMetadataReader.ReadMetadata(NextFile.FullName);

                        //图片宽度
                        float w =float.Parse( rmd.First(p => p.Name == "Exif IFD0").Tags.First(p => p.Name == "Image Width").Description.Split(' ')[0]);

                        //图片高度
                        float h = float.Parse(rmd.First(p => p.Name == "Exif IFD0").Tags.First(p => p.Name == "Image Height").Description.Split(' ')[0]);

                        //经度
                        string lon = rmd.First(p => p.Name == "GPS").Tags.First(p => p.Name == "GPS Longitude").Description;

                         //纬度
                       string lat = rmd.First(p => p.Name == "GPS").Tags.First(p => p.Name == "GPS Latitude").Description;
                       //时间
                        string time = rmd.First(p => p.Name == "Exif IFD0").Tags.First(p => p.Name == "Date/Time").Description;

 

2.将经纬度(度分秒)转换为度,通过百度地图的坐标逆解析服务接口,将经纬度坐标转换为地址;

        //经纬度(度分秒转换)
        private string convertToDot(string value)
        {
            try
            {
                //116° 25' 10.73"
                value = value.Replace("°", "").Replace("'", "").Replace("\"", "").Replace("/", "");

                string[] values = value.Split(' ');

                double v1 = double.Parse(values[0]);

                double v2 = double.Parse(values[1]);

                double v3 = double.Parse(values[2]);

                double result = v1 + v2 / 60 + v3 / 360;

                return result.ToString();
            }
            catch (Exception ex)
            {

                throw;
            }
        }
地址逆解析,此处使用的是百度地图的  

string weiUrl = string.Format("http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location={0},{1}&output=XML&latest_admin=1&ak=你自己的ak"
                    , dbLat, dbLng);

                        string poi = HttpGet(weiUrl);





  public static string HttpGet(string url)
        {
            //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
            Encoding encoding = Encoding.UTF8;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            request.Accept = "text/html, application/xhtml+xml, */*";
            request.ContentType = "application/json";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                return reader.ReadToEnd();
            }
        }

3.拼接想要画在照片上的文字,并通过图片大小计算字体的大小,位置,进行绘制。

 
        //文本在图片上的坐标以及字体大小计算
  public labelAttribute caculateFontSize(float picW, float picH,string lable) {
            try
            {
                float shortL = picH >= picW ? picW : picH;

                float labelLength = shortL / 2;

                float oneSize = labelLength / lable.Length;

                float x = 20;

                float y = picH - oneSize-40;

                labelAttribute lb = new labelAttribute()
                {
                    x=x,
                    y=y,
                    size=oneSize

                };

                return lb;


            }
            catch (Exception EX)
            {

                throw;
            }

        }



        public class labelAttribute {

            public float x { get; set; }

            public float y { get; set; }

            public float size { get; set; }
        }
//绘制
  public static void drawPictures(FileInfo file, string label,labelAttribute  attribute ,string saveDic)
        {
            try
            {

                System.Drawing.Image imgSrc = System.Drawing.Image.FromFile(file.FullName);

                Graphics g = Graphics.FromImage(imgSrc);
                
                    g.DrawImage(imgSrc, 0, 0, imgSrc.Width, imgSrc.Height);

                    using (Font f = new Font("宋体", attribute.size))
                    {
                        using (Brush b = new SolidBrush(Color.Red))
                        {
                            string addText = label;
                            g.DrawString(addText, f, b, attribute.x, attribute.y);
                        }
                    }


                string savePath = string.Format(@"{0}\{1}{2}", saveDic, file.Name.Replace(file.Extension, ""), file.Extension);

                imgSrc.Save(savePath);

            }
            catch (Exception ex)
            {

                throw;
            }


        }

最后展示下效果 

 

传送门:https://github.com/Spe1993/SpeRemarks/tree/master/C%23/照片水印添加(时间、地点%EF%BC%89

 

发布了16 篇原创文章 · 获赞 2 · 访问量 3506

猜你喜欢

转载自blog.csdn.net/weixin_41012454/article/details/88726891