Skip to content

Instantly share code, notes, and snippets.

@openize-words-gists
Created May 22, 2024 12:59
Show Gist options
  • Save openize-words-gists/6a5e459efed49465aab84e33507148af to your computer and use it in GitHub Desktop.
Save openize-words-gists/6a5e459efed49465aab84e33507148af to your computer and use it in GitHub Desktop.
C# Modify Word Document Images
// Prerequisite: Install <a href="https://www.nuget.org/packages/Openize.Words">Openize.Words</a>.
// Load the Word Document
var doc = new Openize.Words.Document($"WordImgs.docx");
var body = new Openize.Words.Body(doc);
foreach (var img in body.Images)
{
var skBitmap = SkiaSharp.SKBitmap.Decode(img.ImageData);
var skImage = SkiaSharp.SKImage.FromBitmap(skBitmap);
var encoded = skImage.Encode(SkiaSharp.SKEncodedImageFormat.Jpeg, 100);
img.ImageData = encoded.ToArray();
img.Height = 250;
img.Width = 200;
doc.Update(img);
}
// Save the modified Word Document
doc.Save($"ModifiedWordImgs.docx");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment