WPF: Modify the metadata of an image (bitmapsource type)

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.