Esempio n. 1
0
        public static void DoIt(string fileName)
        {
            EXIFInfo info = new EXIFInfo();

            /// Add Exif Info to be added/updated
            info.Add(EXIFBase.ExifField.Latitude, "54.675051");
            info.Add(EXIFBase.ExifField.Longitude, "25.273356");
            Console.WriteLine("Starting EXIF edit");

            /// Call the main function
            EXIFBase.ChangeImageExif(fileName, info);
            ExtractMetaData(fileName);
        }
Esempio n. 2
0
        /// <summary>
        /// Change/Add standard EXIF field vlaue
        /// </summary>
        /// <param name="file"></param>
        /// <param name="objExifInfo"></param>
        /// <returns></returns>
        public static bool ChangeImageExif(string file, EXIFInfo objExifInfo)
        {
            #region Vars
            string             sPropValue = string.Empty;
            EXIFBase.ExifField field;
            PropertyItem       propItem   = null;
            ImageFormat        ifOriginal = null;
            Graphics           gSave      = null;
            Image iOriginal = null;
            Image iSave     = null;
            #endregion

            Console.WriteLine("Started");

            try
            {
                iOriginal  = new Bitmap(file);
                ifOriginal = iOriginal.RawFormat;
                Console.WriteLine("Bitmapped file");

                // For each EXIFField in objExifInfo, add it to Image EXIF
                foreach (var exField in objExifInfo)
                {
                    field = (EXIFBase.ExifField)Enum.Parse(typeof(EXIFBase.ExifField), exField.Key.ToString());
                    try
                    {
                        // Get the EXIF value from Image
                        Console.WriteLine("Getting EXIF value from image");
                        propItem   = iOriginal.GetPropertyItem((int)field);
                        sPropValue = System.Text.Encoding.UTF8.GetString(propItem.Value);

                        //Change the value
                        sPropValue = sPropValue.Replace(sPropValue, exField.Value);

                        // Get bytes
                        propItem.Value = System.Text.Encoding.UTF8.GetBytes(sPropValue);

                        //Set the property on the image
                        iOriginal.SetPropertyItem(propItem);
                    }
                    catch (System.ArgumentException)
                    {
                        Console.WriteLine("EXIF param does not exist, creating new");
                        // EXIF tag doesn't exist, add it to image
                        WriteEXIFField(iOriginal, field, exField.Value.ToString() + "\0");
                    }
                }
                //Store the list of properties that exist on the image
                ArrayList alPropertyItems = new ArrayList();

                foreach (PropertyItem pi in iOriginal.PropertyItems)
                {
                    alPropertyItems.Add(pi);
                }

                //Create temp image
                iSave = new Bitmap(iOriginal.Width, iOriginal.Height, iOriginal.PixelFormat);
                Console.WriteLine("Temp img created");
                //Copy the original image over to the temp image
                gSave = Graphics.FromImage(iSave);

                //If you check iSave at this point, it does not have any EXIF properties -
                //only the image gets recreated
                gSave.DrawImage(iOriginal, 0, 0, iOriginal.Width, iOriginal.Height);
                Console.WriteLine(iSave);
                //Get rid of the locks on the original image
                iOriginal.Dispose();
                gSave.Dispose();

                //Copy the original EXIF properties to the new image
                foreach (PropertyItem pi in alPropertyItems)
                {
                    iSave.SetPropertyItem(pi);
                }

                //Save the temp image over the original image
                Console.WriteLine(iSave);
                iSave.Save(file, ifOriginal);
                Console.WriteLine("Pavyko");


                return(true);
            }
            catch (Exception)
            {
                // TODO: Exception logging
                return(false);
            }
            finally
            {
                iSave.Dispose();
            }
        }