/// <summary>
            /// Gets Exif info from Jpeg file
            /// </summary>
            public static void GetExifInfo()
            {
                try
                {
                    //ExStart:GetExifPropertiesJpegImage
                    // initialize JpegFormat
                    JpegFormat jpegFormat = new JpegFormat(Common.MapSourceFilePath(filePath));

                    // get EXIF data
                    JpegExifInfo exif = (JpegExifInfo)jpegFormat.GetExifInfo();

                    if (exif != null)
                    {
                        // get artist
                        Console.WriteLine("Artist: {0}", exif.Artist);
                        // get description
                        Console.WriteLine("Description: {0}", exif.ImageDescription);
                        // get user's comment
                        Console.WriteLine("User Comment: {0}", exif.UserComment);
                        // get user's Model
                        Console.WriteLine("Model: {0}", exif.Model);
                        // get user's Make
                        Console.WriteLine("Make: {0}", exif.Make);
                        // get user's CameraOwnerName
                        Console.WriteLine("CameraOwnerName: {0}", exif.CameraOwnerName);
                    }
                    //ExEnd:GetExifPropertiesJpegImage
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
            }
Exemplo n.º 2
0
        /// <summary>
        /// Takes manufacturer as input and returns photos made on particular camera
        /// </summary>
        /// <param name="manufacturer">Camera manufacturer name</param>
        public void FilterByCameraManufacturer(string manufacturer)
        {
            // Map directory in source folder
            string sourceDirectoryPath = Common.MapSourceFilePath(this.PhotosDirectory);

            // get jpeg files
            string[] files = Directory.GetFiles(sourceDirectoryPath, "*.jpg");

            List <string> result = new List <string>();

            foreach (string path in files)
            {
                // recognize file
                FormatBase format = FormatFactory.RecognizeFormat(path);

                // casting to JpegFormat
                if (format is JpegFormat)
                {
                    JpegFormat jpeg = (JpegFormat)format;

                    // get exif data
                    JpegExifInfo exif = (JpegExifInfo)jpeg.GetExifInfo();

                    if (exif != null)
                    {
                        if (string.Compare(exif.Make, manufacturer, StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            // add file path to list
                            result.Add(Path.GetFileName(path));
                        }
                    }
                }
            }
            Console.WriteLine(string.Join("\n", result));
        }
            /// <summary>
            /// Updates Exif info of Jpeg file and creates output file
            /// </summary>
            public static void UpdateExifInfo()
            {
                try
                {
                    //ExStart:UpdateExifPropertiesJpegImage
                    // initialize JpegFormat
                    JpegFormat jpegFormat = new JpegFormat(Common.MapSourceFilePath(filePath));

                    // get EXIF data
                    JpegExifInfo exif = (JpegExifInfo)jpegFormat.GetExifInfo();
                    if (exif == null)
                    {
                        // initialize EXIF data if null
                        exif = new JpegExifInfo();
                    }

                    // set artist
                    exif.Artist = "Usman";
                    // set make
                    exif.Make = "ABC";
                    // set model
                    exif.Model = "S120";
                    // set the name of the camera's owner
                    exif.CameraOwnerName = "Owner";
                    // set description
                    exif.ImageDescription = "sample description";

                    // update EXIF data
                    jpegFormat.SetExifInfo(exif);

                    // commit changes
                    jpegFormat.Save(Common.MapDestinationFilePath(filePath));
                    //ExEnd:UpdateExifPropertiesJpegImage
                    Console.WriteLine("File saved in destination folder.");
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
            }
            /// <summary>
            /// Read All Exif tags
            /// </summary>
            /// 
            public static void ReadAllExifTags()
            {
                JpegFormat jpegFormat = new JpegFormat(Common.MapSourceFilePath(filePath));

                // get EXIF data
                ExifInfo exifInfo = jpegFormat.GetExifInfo();
                if (exifInfo != null)
                {
                    TiffTag[] allTags = exifInfo.Tags;

                    foreach (TiffTag tag in allTags)
                    {
                        switch (tag.TagType)
                        {
                            case TiffTagType.Ascii:
                                TiffAsciiTag asciiTag = tag as TiffAsciiTag;
                                Console.WriteLine("Tag: {0}, value: {1}", asciiTag.DefinedTag, asciiTag.Value);
                                break;

                            case TiffTagType.Rational:
                                TiffRationalTag rationalTag = tag as TiffRationalTag;
                                Console.WriteLine("Tag: {0}, value: {1}", rationalTag.DefinedTag, rationalTag.Value);
                                break;
                        }//end of switch
                    }//end of foreach
                }//end of if (exifInfo != null)

            }
            /// <summary>
            /// Read Specific Exif tag
            /// </summary>
            /// 
            public static void ReadExifTag()
            {
                JpegFormat jpegFormat = new JpegFormat(Common.MapSourceFilePath(filePath));

                // get EXIF data
                ExifInfo exifInfo = jpegFormat.GetExifInfo();
                if (exifInfo != null)
                {
                    // get specific tag using indexer
                    TiffAsciiTag artist = (TiffAsciiTag)exifInfo[TiffTagIdEnum.Artist];
                    if (artist != null)
                    {
                        Console.WriteLine("Artist: {0}", artist.Value);
                    }
                }

            }
            /// <summary>
            /// Updates Exif info of Jpeg file and creates output file
            /// </summary> 
            public static void UpdateExifInfo()
            {
                try
                {
                    //ExStart:UpdateExifPropertiesJpegImage
                    // initialize JpegFormat
                    JpegFormat jpegFormat = new JpegFormat(Common.MapSourceFilePath(filePath));

                    // get EXIF data
                    JpegExifInfo exif = (JpegExifInfo)jpegFormat.GetExifInfo();
                    if (exif == null)
                    {
                        // initialize EXIF data if null
                        exif = new JpegExifInfo();
                    }

                    // set artist
                    exif.Artist = "Usman";
                    // set make
                    exif.Make = "ABC";
                    // set model
                    exif.Model = "S120";
                    // set the name of the camera's owner
                    exif.CameraOwnerName = "Owner";
                    // set description
                    exif.ImageDescription = "sample description";

                    // update EXIF data
                    jpegFormat.SetExifInfo(exif);

                    // commit changes
                    jpegFormat.Save(Common.MapDestinationFilePath(filePath));
                    //ExEnd:UpdateExifPropertiesJpegImage
                    Console.WriteLine("File saved in destination folder.");
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
            }
            /// <summary>
            /// Gets Exif info from Jpeg file
            /// </summary> 
            public static void GetExifInfo()
            {
                try
                {
                    //ExStart:GetExifPropertiesJpegImage
                    // initialize JpegFormat
                    JpegFormat jpegFormat = new JpegFormat(Common.MapSourceFilePath(filePath));

                    // get EXIF data
                    JpegExifInfo exif = (JpegExifInfo)jpegFormat.GetExifInfo();

                    if (exif != null)
                    {
                        // get artist 
                        Console.WriteLine("Artist: {0}", exif.Artist);
                        // get description 
                        Console.WriteLine("Description: {0}", exif.ImageDescription);
                        // get user's comment 
                        Console.WriteLine("User Comment: {0}", exif.UserComment);
                        // get user's Model 
                        Console.WriteLine("Model: {0}", exif.Model);
                        // get user's Make 
                        Console.WriteLine("Make: {0}", exif.Make);
                        // get user's CameraOwnerName 
                        Console.WriteLine("CameraOwnerName: {0}", exif.CameraOwnerName);
                        // get longitude
                        Console.WriteLine("Longitude: {0}", exif.GPSData.Longitude[0].ToString());
                        // get latitude
                        Console.WriteLine("Latitude: {0}", exif.GPSData.Latitude[0].ToString());
                    }
                    //ExEnd:GetExifPropertiesJpegImage
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
            }