How to Add Image to DOCX in C

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

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronWord with NuGet Package Manager

    PM > Install-Package IronWord

  2. Copy 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");
  3. Deploy 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.

TipsInsert an image as a child element within a paragraph for better document hierarchy. The paragraph defines text wrapping and other text formatting properties. This approach provides more control over image positioning and allows images to flow with surrounding text.

:path=/static-assets/word/content-code-examples/how-to/add-image-insert-image.cs
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");
$vbLabelText   $csharpLabel
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.

:path=/static-assets/word/content-code-examples/how-to/add-image-insert-image-via-http-stream.cs
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;

// 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 = client.GetStreamAsync("https://api.example.com/secure/image.png");
    doc.AddImage(authenticatedStream);
}

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

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.

SettingsDescriptionExample
WidthHorizontal dimension of the image in pixelsimage.Width = 500;
HeightVertical dimension of the image in pixelsimage.Height = 300;
WrapTextText wrapping behavior around the imageimage.WrapText = WrapText.Square;
DistanceFromLeftSpacing measurement from left edge in pixelsimage.DistanceFromLeft = 10;
DistanceFromRightSpacing measurement from right edge in pixelsimage.DistanceFromRight = 10;
DistanceFromTopSpacing measurement from top edge in pixelsimage.DistanceFromTop = 15;
DistanceFromBottomSpacing measurement from bottom edge in pixelsimage.DistanceFromBottom = 15;
PositionSpatial placement information (X and Y coordinates)image.Position = new ElementPosition(50, 100);
ScaleProportional sizing factors for X and Y axesimage.Scale = new PointF(1.5f, 1.5f);
TranslateDisplacement coordinates for repositioningimage.Translate = new PointF(20, 30);

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.

:path=/static-assets/word/content-code-examples/how-to/add-image-custom-size.cs
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.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");
$vbLabelText   $csharpLabel
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 (scale/translate) - 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 do I add an image to a DOCX file in C#?

With IronWord, you can add images to DOCX files by creating an ImageContent instance with your image file path, then using the AddImage() method. IronWord supports common formats like JPG, PNG, BMP, TIFF, and GIF, making it easy to insert images programmatically into Word documents.

What image formats are supported when adding images to Word documents?

IronWord supports all major image formats including .jpg, .png, .bmp, .tiff, and .gif files. Each format maintains its quality when inserted - JPEG for photographs, PNG for graphics with transparency, BMP for uncompressed quality, TIFF for high-quality print documents, and GIF for simple animations (displaying only the first frame).

Can I control the size and positioning of images in DOCX files?

Yes, IronWord's ImageContent class allows you to customize image properties like width, height, and text wrapping. You can modify dimensions and positioning before insertion, giving you full control over how images appear in your Word documents.

How do I insert an image within a paragraph for better document structure?

IronWord allows you to insert images as child elements within paragraphs, providing better document hierarchy and control over text wrapping. This approach integrates images with surrounding text flow and gives you more precise positioning options compared to adding images without a parent node.

What's the quickest way to add an image to a Word document programmatically?

The quickest method with IronWord is to create a WordDocument instance, load your image using new ImageContent('photo.jpg'), call doc.AddImage(image), and save with doc.SaveAs('document-with-image.docx'). This simple four-step process handles the entire image insertion workflow.

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.

Before joining the Iron Software team, Ahmad worked on automation projects ...

Read More
Ready to Get Started?
Nuget Downloads 29,064 | Version: 2025.12 just released