IRONSOFTWAREHOME

How to Add Image to DOCX in C#

Ahmad Sohail
Ahmad Sohail
Updated: August 2, 2026

IronWord provides the ImageContent class to insert images (.jpg, .png, .bmp, .tiff, .gif) into DOCX files with customizable properties like width, height, and text wrapping. Use IronWord to add images to Word documents for document automation and report generation.

Quickstart: Add Image to DOCX in C#
  1. Install IronWord via NuGet Package Manager
  2. Create a new WordDocument instance
  3. Load your image using ImageContent class
  4. Add the image to the document using AddImage()
  5. Save the document as DOCX
  1. 1Install IronWord with NuGet Package Manager

    PM > Install-Package IronWord

  2. 2Copy and run this code snippet.

    using IronWord;
    using IronWord.Models;
    
    // Create new document
    WordDocument doc = new WordDocument();
    
    // Add image
    ImageContent image = new ImageContent("photo.jpg");
    doc.AddImage(image);
    
    // Save document
    doc.SaveAs("document-with-image.docx");
    C#
  3. 3Deploy to test on your live environment

    Start using IronWord in your project today with a free trial
    arrow pointer

Try IronWord


How Do I Add an Image to DOCX?

Reference an image using its file path. First, instantiate the ImageContent class with the file path as a string. Use the image variable throughout the file to modify properties like width and height. Add the image to the .docx file using the AddImage() function. Export and save the document locally.

The example below adds an image to the document without any parent node. Supported file formats include .jpg, .png, .bmp, .tiff, and .gif. This flexibility lets you work with any common image format.

Tips: Insert an image and configure its width and height before adding it directly to the document. This approach lets you set dimensional properties prior to insertion.
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;

// initializing docx file
WordDocument doc = new IronWord.WordDocument();

// instantiating image file
IronWord.Models.ImageContent image = new IronWord.Models.ImageContent("sample-image.jpg");

// modifying image properties
image.Width = 200;
image.Height = 200;

// AddImage function saving the image
doc.AddImage(image);

// Save and export the file
doc.SaveAs("inserted-image.docx");
Word document showing inserted nighttime city skyline image with Home tab ribbon displaying formatting options

Which Image Formats Are Supported?

Supported file formats: .jpg, .png, .bmp, .tiff, and .gif. Each format maintains its quality when inserted. JPEG works best for photographs. PNG supports transparency for logos and graphics. BMP provides uncompressed quality. TIFF suits high-quality print documents. GIF allows simple animations (only the first frame displays in static documents).

Where Is the Image Placed in the Document?

Images are added at the current cursor position by default, without any parent node. For precise positioning, insert images as child elements within paragraphs. This provides better control over text flow and integrates images with your document's structure.

Why Use ImageContent Class?

The ImageContent class manages image properties in a structured way. Modify dimensions, positioning, and formatting before insertion. This approach ensures consistency across your document generation process and applies standard formatting rules throughout your application. The class encapsulates all image-related properties, making code more maintainable and reducing formatting errors.

How Do I Add Images via Stream?

Local or static URL images are easy to add using the previous method. However, applications often work with images from databases, web services, or dynamically generated content. Use the Stream method to add images behind secure APIs requiring authentication.

The example below shows an HTTP client sending authorization tokens to retrieve an authenticated image stream. The stream integrates directly into the document before export. This approach eliminates temporary file storage and improves security for sensitive image data.

using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;

// initializing docx file
WordDocument doc = new IronWord.WordDocument();

using (HttpClient client = new HttpClient())
{
    // Add authentication headers
    client.DefaultRequestHeaders.Add("Authorization", "Bearer YOUR_API_KEY_HERE");
    client.DefaultRequestHeaders.Add("User-Agent", "MyApp/1.0");

    // Get image from authenticated endpoint
    Stream authenticatedStream = await client.GetStreamAsync("https://api.example.com/secure/image.png");
    doc.AddImage(authenticatedStream);
}

// Export docx
doc.SaveAs("added-image-via-http-stream.docx");

When Should I Use Stream Method?

Use the Stream method when:

  • Images are behind secure APIs requiring authentication
  • Processing images dynamically from memory
  • Working with images stored as binary data in databases

This method works well in enterprise applications where images are stored in document management systems, cloud storage, or generated by image processing services.

What Are the Benefits of Stream Loading?

Stream loading integrates images from authenticated endpoints without saving temporary files. This improves security and performance. Benefits include:

  • Reduced disk I/O operations
  • No sensitive image caching on disk
  • Real-time image processing workflows
  • Better memory management for large images
  • Flexible image source options

How Can I Modify Image Properties?

IronWord provides comprehensive methods to customize image properties. Adjust these properties before or after adding the image to the document.

Please note: Width, Height, and the DistanceFrom* properties default to points (1 inch = 72 points) when assigned directly, as shown below. To work in a different unit, use the corresponding Get/Set overload instead — for example, image.SetWidth(5, MeasurementUnit.Centimeter).
SettingsDescriptionExample
WidthHorizontal dimension of the image, in points (1 inch = 72 points)image.Width = 500;
HeightVertical dimension of the image, in points (1 inch = 72 points)image.Height = 300;
TextWrapBehaviorText wrapping behavior around the image. Its type is the ITextWrapBehavior interface; no public implementing class or factory is currently documented for constructing a value, so no working example is shown here pending product-team confirmation.-
DistanceFromLeftSpacing measurement from left edge, in pointsimage.DistanceFromLeft = 10;
DistanceFromRightSpacing measurement from right edge, in pointsimage.DistanceFromRight = 10;
DistanceFromTopSpacing measurement from top edge, in pointsimage.DistanceFromTop = 15;
DistanceFromBottomSpacing measurement from bottom edge, in pointsimage.DistanceFromBottom = 15;
PositionSpatial placement information (X and Y coordinates)var pos = new ElementPosition(); pos.SetXPosition(50, MeasurementUnit.Point); pos.SetYPosition(100, MeasurementUnit.Point); image.Position = pos;

How Do I Customize Width & Height?

Implement custom width and height by altering the aspect ratio. Control how images appear in documents whether maintaining proportions or fitting into specific layout constraints.

using IronWord;

// initializing docx file
WordDocument doc = new IronWord.WordDocument();

// instantiating image file
IronWord.Models.ImageContent image = new IronWord.Models.ImageContent("sample-image.tiff");

// modifying the aspect ratio by introducing custom width
image.Width = 800;
image.Height = 200;

// AddImage function saving the image
doc.AddImage(image);

// Save and export the file
doc.SaveAs("custom-size-image.docx");
Word document showing laptop image with 'Tagged Image File F' overlay text demonstrating image property display

What Happens to Aspect Ratio?

Custom width and height values override the original aspect ratio. Stretch or compress images to fit layout requirements like headers, sidebars, or fixed-size containers. Extreme distortion can look unprofessional. To maintain aspect ratio while resizing, calculate proportional dimensions based on your target size.

Which Properties Should I Set First?

Set properties in this order:

  1. Dimensions (width/height) - foundation of image layout
  2. Positioning (DistanceFrom properties) - control spacing and margins
  3. Advanced properties (position, text wrap) - fine-tuning

This approach ensures each property builds logically on previous ones. Some properties interact - text wrapping affects how distance properties work.

Frequently Asked Questions

How can I add an image to a DOCX file using IronWord?

To add an image to a DOCX file using IronWord, initiate a `WordDocument`. Load your image with the `ImageContent` class, configure the image properties like width and height if necessary, and use the `AddImage()` method to insert it into the document.

What image formats does IronWord support for DOCX files?

IronWord supports various image formats including .jpg, .png, .bmp, .tiff, and .gif. This allows you to maintain image quality while inserting different types of images into your DOCX files.

Is it possible to customize image dimensions in IronWord before adding to DOCX?

Yes, you can customize image dimensions by setting the `Width` and `Height` properties of the `ImageContent` class before adding the image to the DOCX file.

How do I handle image insertion from authenticated APIs in DOCX using IronWord?

Use the `Stream` method within IronWord to load images from authenticated APIs. This method allows integrating images directly into DOCX files using secure, real-time processing without needing to save image files temporarily.

What are the advantages of using the `Stream` method for image loading in IronWord?

The `Stream` method enhances security and performance by reducing disk I/O, avoiding temporary file storage, enabling real-time workflows, and improving memory management for large images.

How do I place images at specific positions within a DOCX file using IronWord?

Images in IronWord are initially placed at the current cursor position. For precise positioning, insert images as child elements within paragraphs to manage text flow and layout.

Can I modify text wrapping settings for images in IronWord?

Yes, IronWord allows you to set text wrapping for images using the `TextWrapBehavior` property. This controls how text flows around the inserted images within the DOCX file.

Why is it beneficial to use the `ImageContent` class in IronWord?

The `ImageContent` class in IronWord provides a structured way to manage and customize image attributes such as dimensions and positioning before adding them to DOCX files, ensuring consistency and reducing formatting errors.

How do I maintain the aspect ratio of images when resizing in IronWord?

To maintain the aspect ratio while resizing an image in IronWord, calculate proportional dimensions based on the target size to avoid distortion and create a professional presentation.

What sequence should I follow when setting image properties in IronWord?

It's recommended to set properties in this order in IronWord: first dimensions (width/height), then spacing and margins (DistanceFrom properties), and finally advanced settings like position and text wrapping for comprehensive image layout control.

Ahmad Sohail
Full Stack Developer

Ahmad is a full-stack developer with a strong foundation in C#, Python, and web technologies. He has a deep interest in building scalable software solutions and enjoys exploring how design and functionality meet in real-world applications.

...
Read More

Ready to Get Started?

Nuget Downloads 56,401Version:2026.9just released

Get your free 30-day Trial Key instantly.
No credit card or account creation required
C# NuGet Library for PDF
Install with NuGet

Version: 2026.9

PM > Install-Package IronWord
nuget.org/packages/IronWord/
  1. In Solution Explorer, right-click References, Manage NuGet Packages
  2. Select Browse and search "IronWord"
  3. Select the package and install
C# PDF DLL
Download DLL

Version: 2026.9

  1. Download and unzip IronWord to a location such as ~/Libs within your Solution directory
  2. In Visual Studio Solution Explorer, right click References. Select Browse, "IronWord.dll"

Licenses from $999

Key in blue circle

Get your free 30-day Trial Key instantly.

Your trial license will be sent to your email address

No limitations. 100% unlocked. No credit card.

bullet_checkedNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
Book your free Live Demo
Booking Badge

Trusted by Millions of Engineers Worldwide

Iron Software's customer logos
Get Your No-Obligation Consult
Complete the form below or email sales@ironsoftware.com
Your details will always be kept confidential.
Trusted by Millions of Engineers Worldwide
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required