Problem Statement:
Sometimes user has to upload or attach image inside Notes Section of form. When user uploads any image in Notes, dynamics stores image in Base 64 format. As frequency of uploading/attaching images increase then it will also increase the size requirement in CDS. To reduce the size requirement in CDS, we can compress image while storing it in CDS.
Solution:
When user attaches Image in Note(annotation), we can trigger Post Operation Async Plugin to Compress Image. When image gets attached to Notes, it gets stored as Base 64 inside “documentbody” field. So, we have to trigger Plugin on ‘Create of Notes’, get Base 64 code of Image, Compress Image and Update New Base 64 Code in Notes.
Following Plugin code Converts Base 64 Code to Image format compresses Image without loosing quality and then reconvert it to Base 64 code:
Plugin Code: (JPEG)
- Dynamics store Image attached in Note(annotations) in Base 64 form. Image Body will be stored in “documentbody” field and Image Type will be stored in “mimetype” field. Get Image Body and Image Type using following code:

- Now, create following method, which will compress Base 64 image. We have to pass Base 64 code and quality to custom method which will be used to compress image. Using this method, we can compress image till 10% – 90%. So, pass Image Quality between 10-90.

- Add Encoder method in class as it is used in “Base64StringImage” method.

- Once Base 64 Image Code gets compressed, our method will provide new Base 64 Image Code. Store it in new variable.

- Now, Update Document Body of Note(annotations) with new Document Body.

Plugin Step:
After Registering our code as New Plugin Assembly, add Step on Create on annotation (Notes). Make Execution Mode as Asynchronous as there will be chances where multiple users will upload Images as same time.

Example:
Image with Size: 1465 KB

Uploaded Image in Notes:

Image After Compression:

Conclusion:
Using above the plugin on Annotation Entity we can effectively compress image size by 10% to 90%. This will help us to save CDS size.
Thanks Mayur Hande for your inputs on this blogs.
2 thoughts on “Compressing Image Attached in Notes”