Add a watermark to your photos

Chinese New Year this year, go out to play, go home and want to pick out a photo lab incorporated into the album, but a long time to forget fear when shooting pictures where taken, then hands-on writing a small program, add a watermark to the photo time and place. End of the article attached program portal

The main idea:

1. Exif information being read, from which acquired photo shoot time, latitude and longitude (degrees minutes and seconds), picture width, height and other information;

Library use 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. The latitude and longitude (degrees minutes and seconds) converted to a degree, by Baidu map coordinates inverse parsing service interface, convert latitude and longitude coordinates for the address;

        //经纬度(度分秒转换)
        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. Want to draw on a photo mosaic of the text, and calculates the font size, position by image size, draw.

 
        //文本在图片上的坐标以及字体大小计算
  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;
            }


        }

The last show under the effect of 

 

 

Portal: https://github.com/Spe1993/SpeRemarks/tree/master/C%23/ photo watermarking (time, location% EF% BC% 89

 

Published 16 original articles · won praise 2 · Views 3506

Guess you like

Origin blog.csdn.net/weixin_41012454/article/details/88726891