In short, you need to first obtain the image metadata, then set the bitmapframe. Metadata attribute, and then save the image with bitmapencoder.
Note some details here. First, if you use bitmapframe. Create (bitmapsource) to initialize bitmapframe:
Bitmapframe. Create (New bitmapimage (New uri (@ "C: \ Users \ mgen \ Desktop \ a.jpg ")));
In this case, the bitmapframe does not contain metadata, and the metadata attribute returns NULL.
Therefore, bitmapdecoder must be used to obtain the image metadata. bitmapframe. Metadata in bitmapdecoder. frames can be used to obtain the metadata.
If you directly modify the obtained bitmapmetadata object, an exception is thrown: (specified value of Type 'System. windows. media. imaging. bitmapmetadata 'must have isfrozen set to false to modify .), the reason is that the bitmapmetadata type also belongs to the freezable type, and isfrozen is still true, so it cannot be modified. You can use the clone method to clone a freezable object whose isfrozen is false.
After understanding the above information, the source code is easy to understand. The code:
// Source file
VaR src = @ "C: \ Users \ mgen \ Desktop \ a.jpg ";
// Save the file
VaR DEST = @ "C: \ Users \ mgen \ Desktop \ B .jpg ";
// Create bitmapencoder
VaR bitmapencoder = new bitmapencoder ();
// Create bitmapdecoder
VaR bitmapdecoder = new bitmapdecoder (New uri (SRC), bitmapcreateoptions. None, bitmapcacheoption. Default );
// Obtain metadata (bitmapmetadata object)
VaR frame = bitmapdecoder. Frames [0];
// Clone the modifiable freezable object
VaR metadata = (bitmapmetadata) frame. Metadata. Clone ();
// Modify metadata
Metadata. cameramanufacturer = "mgen! ";
// Create a new bitmapframe for bitmapdecoder
VaR newframe = bitmapframe. Create (frame, frame. thumbnail, metadata, frame. colorcontexts );
// Add bitmapframe
Bitmapencoder. frames. Add (newframe );
// Save the file
Bitmapencoder. Save (system. Io. file. openwrite (DEST ));
Result: