private void ReadMetadata(string filename, bool openForEditing) { // Save filename this.sourceFilename = filename; this.destinationfilename = filename; this.openForEditing = openForEditing; // If open for editing, do additional checks if (openForEditing) { // Validate the threading model WpfFileManager.ValidateThreadingModel(); // The file must be JPG because the TIFF padding queries are different WpfFileManager.ValidateFileIsJpeg(filename); // Create backup of the file so you can read and write to different files string tempFile = filename + ".fotoflytemp"; File.Copy(filename, tempFile, true); // Replace the filename with the temp filename this.sourceFilename = tempFile; } this.ReadMetadata(); if (openForEditing) { // Ensure the metadata has the right padding in place for new data this.AddMetadataPadding(); } }
public static void WriteBitmapMetadata(string outputFile, BitmapMetadata bitmapMetadata, int retryCount) { // Check file exists and is a valid jpg\jpeg file WpfFileManager.ValidateFileIsJpeg(outputFile); // Validate Threading Model WpfFileManager.ValidateThreadingModel(); // Source file is is used as source of the the image & thumbnail for the new file string sourceFile = outputFile.ToLower().Replace(".jpg", ".fotoflytmp"); // Try saving the file as needed bool fileSaved = false; while (true) { if (fileSaved || retryCount == 0) { break; } // Copy file so we have a source file try { if (File.Exists(sourceFile)) { File.Delete(sourceFile); } File.Move(outputFile, sourceFile); } catch (Exception e) { throw new Exception("Unable to create the backup file.", e); } try { WpfFileManager.WriteBitmapMetadata(outputFile, bitmapMetadata, sourceFile); File.Delete(sourceFile); fileSaved = true; } catch (Exception e) { Debug.WriteLine(e.ToString()); fileSaved = false; retryCount--; } // Save file failed so restore files to original location if (!fileSaved) { try { File.Delete(outputFile); File.Move(sourceFile, outputFile); } catch (Exception e) { throw new Exception("Unable to recover from failed save\n" + e); } } } if (!fileSaved) { throw new Exception("Unable to save the file:\n\n" + outputFile); } }
public static void WriteBitmapMetadata(string outputFile, BitmapMetadata bitmapMetadata) { WpfFileManager.WriteBitmapMetadata(outputFile, bitmapMetadata, 3); }