Exemplo n.º 1
0
        public static void WritePhotoMetadataToXml(PhotoMetadata photoMetadata, string fileName)
        {
            int retryCount = 3;

            if (string.IsNullOrEmpty(fileName))
            {
                throw new Exception("FileName is not valid");
            }

            Exception serializerException = null;
            bool      serializerSucceeded = false;

            for (int i = 0; i < retryCount; i++)
            {
                // Try saving the file
                try
                {
                    using (FileStream fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        using (StreamWriter writer = new StreamWriter(fileStream))
                        {
                            // Create the seraliser
                            XmlSerializer xmlSerializer = new XmlSerializer(typeof(PhotoMetadata));

                            // Write the file
                            xmlSerializer.Serialize(writer, photoMetadata, PhotoMetadataTools.XmlSerializerNamespaces);
                        }

                        // Try and force the file lock to be released
                        fileStream.Close();
                        fileStream.Dispose();
                    }

                    serializerSucceeded = true;
                }
                catch (Exception e)
                {
                    serializerException = e;
                    serializerSucceeded = false;
                }

                if (serializerSucceeded)
                {
                    break;
                }
                else
                {
                    // Sleep before trying again
                    Thread.Sleep(PhotoMetadataTools.serializationSleepTime);
                }
            }

            if (serializerSucceeded == false)
            {
                throw new Exception("Unable to save the file: " + fileName, serializerException);
            }
        }
Exemplo n.º 2
0
        public static void WriteBitmapMetadata(BitmapMetadata bitmapMetadata, PhotoMetadata photoMetadata)
        {
            FileMetadata fileMetadata = new FileMetadata(bitmapMetadata);

            // List of changes, used for debugging
            List <CompareResult> compareResults = new List <CompareResult>();

            // Use Reflection to Copy all values from photoMetadata to FileMetadata
            PhotoMetadataTools.UseReflection(photoMetadata, fileMetadata, true, ref compareResults);
        }
Exemplo n.º 3
0
        public static PhotoMetadata ReadBitmapMetadata(BitmapMetadata bitmapMetadata, BitmapDecoder bitmapDecoder)
        {
            PhotoMetadata photoMetadata = new PhotoMetadata();

            // Load Metadata Reader
            FileMetadata fileMetadata = new FileMetadata(bitmapMetadata);

            // List of changes, used for debugging
            List <CompareResult> compareResults = new List <CompareResult>();

            PhotoMetadataTools.UseReflection(fileMetadata, photoMetadata, true, ref compareResults);

            // Use Reflection to Copy all values from fileMetadata to photoMetadata
            return(photoMetadata);
        }