Set Transeperant Color for Images in Word in C#

This tutorial is going to show you how to make a color of a image transeperant using Spire.Doc.

Below screenshot shows an example image with black and white colors:

Set Transeperant Color for Images in Word in C#

Detail steps:

Step 1: Instantiate a Document object and Load the Word file.

Document doc = new Document();
doc.LoadFromFile("Input.docx");

Step 2: Get the first Paragraph in the first section.

Paragraph paragraph = doc.Sections[0].Paragraphs[0];

Step 3: Set the black color of the image(s) in the paragraph to transperant.

foreach (DocumentObject obj in paragraph.ChildObjects)
{
    if (obj is DocPicture)
    {
        (obj as DocPicture).TransparentColor = Color.Black;
    }
}

Step 4: Save the file.

doc.SaveToFile("Result.docx", FileFormat.Docx2013);

Screenshot:

Set Transeperant Color for Images in Word in C#

Full code:

//Instantiate a Document object
Document doc = new Document();
//Load the Word file
doc.LoadFromFile("Input.docx");

//Get the first paragraph in the first section
Paragraph paragraph = doc.Sections[0].Paragraphs[0];

//Set the black color of the image(s) in the paragraph to transperant
foreach (DocumentObject obj in paragraph.ChildObjects)
{
    if (obj is DocPicture)
    {
        (obj as DocPicture).TransparentColor = Color.Black;
    }
}

//Save the file
doc.SaveToFile("Result.docx", FileFormat.Docx2013);