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#
- Install IronWord via NuGet Package Manager
- Create a new
WordDocumentinstance - Load your image using
ImageContentclass - Add the image to the document using
AddImage() - Save the document as DOCX
Get started making PDFs with NuGet now:
Install IronWord with NuGet Package Manager
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");Deploy to test on your live environment
Try IronWord
How to Add an Image to DOCX
- Download the latest stable version of IronWord
- Initialize a new Word document
- Define an image object (.bmp, .jpg, .png, or other supported formats)
- Add the image to the document
- Save and export the document file
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.
:path=/static-assets/word/content-code-examples/how-to/add-image-insert-image.csusing 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");
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.csusing 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");
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.
| Settings | Description | Example |
|---|---|---|
| Width | Horizontal dimension of the image in pixels | image.Width = 500; |
| Height | Vertical dimension of the image in pixels | image.Height = 300; |
| WrapText | Text wrapping behavior around the image | image.WrapText = WrapText.Square; |
| DistanceFromLeft | Spacing measurement from left edge in pixels | image.DistanceFromLeft = 10; |
| DistanceFromRight | Spacing measurement from right edge in pixels | image.DistanceFromRight = 10; |
| DistanceFromTop | Spacing measurement from top edge in pixels | image.DistanceFromTop = 15; |
| DistanceFromBottom | Spacing measurement from bottom edge in pixels | image.DistanceFromBottom = 15; |
| Position | Spatial placement information (X and Y coordinates) | image.Position = new ElementPosition(50, 100); |
| Scale | Proportional sizing factors for X and Y axes | image.Scale = new PointF(1.5f, 1.5f); |
| Translate | Displacement coordinates for repositioning | image.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.csusing 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");
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:
- Dimensions (width/height) - foundation of image layout
- Positioning (DistanceFrom properties) - control spacing and margins
- 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.






