DocumentBuilderInsertImage Method (Image)
Inserts an image from a .NET Image object into the document. The image is inserted inline and at 100% scale.

Namespace: Aspose.Words
Assembly: Aspose.Words (in Aspose.Words.dll) Version: 20.1
Syntax
public Shape InsertImage(
	Image image
)

Parameters

image
Type: System.DrawingImage
The image to insert into the document.

Return Value

Type: Shape
The image node that was just inserted.
Remarks

You can change the image size, location, positioning method and other settings using the Shape object returned by this method.

Examples
Shows how to insert a .NET Image object into a document. The image is inserted inline and at 100% scale (.NetStandard 2.0).
// This creates a builder and also an empty document inside the builder
DocumentBuilder builder = new DocumentBuilder();

// Insert a raster image
// SKBitmap doesn't allow to insert a metafiles
using (SKBitmap rasterImage = SKBitmap.Decode(ImageDir + "Aspose.Words.gif"))
{
    builder.Write("Raster image: ");
    builder.InsertImage(rasterImage);
    builder.Writeln();
}

builder.Document.Save(ArtifactsDir + "Image.CreateFromImage.doc");
Examples
Shows different solutions of how to import an image into a document from Image class.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Image image = Image.FromFile(ImageDir + "Aspose.Words.gif");

builder.Writeln("\nInserted image from Image class: ");
builder.InsertImage(image);

builder.Writeln("\nInserted image from Image class with a custom size: ");
builder.InsertImage(image, ConvertUtil.PixelToPoint(250), ConvertUtil.PixelToPoint(144));

builder.Writeln("\nInserted image from Image class using relative positions: ");
builder.InsertImage(image, RelativeHorizontalPosition.Margin, 100, RelativeVerticalPosition.Margin,
    100, 200, 100, WrapType.Square);

doc.Save(ArtifactsDir + "InsertImageFromImageClass.docx");
Examples
Shows how to insert a .NET Image object into a document. The image is inserted inline and at 100% scale.
// This creates a builder and also an empty document inside the builder
DocumentBuilder builder = new DocumentBuilder();

// Insert a raster image
Image rasterImage = Image.FromFile(ImageDir + "Aspose.Words.gif");
try
{
    builder.Write("Raster image: ");
    builder.InsertImage(rasterImage);
    builder.Writeln();
}
finally
{
    rasterImage.Dispose();
}

// Aspose.Words allows to insert a metafile too
Image metafile = Image.FromFile(ImageDir + "Hammer.wmf");
try
{
    builder.Write("Metafile: ");
    builder.InsertImage(metafile);
    builder.Writeln();
}
finally
{
    metafile.Dispose();
}

builder.Document.Save(ArtifactsDir + "Image.CreateFromImage.doc");
Examples
Shows different solutions of how to import an image into a document from Image class (.NetStandard 2.0).
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

using (SKBitmap bitmap = SKBitmap.Decode(ImageDir + "Aspose.Words.gif"))
{
    builder.Writeln("\nInserted image from Image class: ");
    builder.InsertImage(bitmap);

    builder.Writeln("\nInserted image from Image class with a custom size: ");
    builder.InsertImage(bitmap, ConvertUtil.PixelToPoint(250), ConvertUtil.PixelToPoint(144));

    builder.Writeln("\nInserted image from Image class using relative positions: ");
    builder.InsertImage(bitmap, RelativeHorizontalPosition.Margin, 100, RelativeVerticalPosition.Margin,
        100, 200, 100, WrapType.Square);
}

doc.Save(ArtifactsDir + "InsertImageFromImageClass.NetStandard2.docx");
Examples
Inserts a watermark image into a document using DocumentBuilder.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// The best place for the watermark image is in the header or footer so it is shown on every page
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);

Image image = Image.FromFile(ImageDir + "Watermark.png");

// Insert a floating picture
Shape shape = builder.InsertImage(image);
shape.WrapType = WrapType.None;
shape.BehindText = true;

shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
shape.RelativeVerticalPosition = RelativeVerticalPosition.Page;

// Calculate image left and top position so it appears in the center of the page
shape.Left = (builder.PageSetup.PageWidth - shape.Width) / 2;
shape.Top = (builder.PageSetup.PageHeight - shape.Height) / 2;

doc.Save(ArtifactsDir + "DocumentBuilder.InsertWatermark.doc");
Examples
Inserts a watermark image into a document using DocumentBuilder (.NetStandard 2.0).
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// The best place for the watermark image is in the header or footer so it is shown on every page
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);

using (SKBitmap image = SKBitmap.Decode(ImageDir + "Watermark.png"))
{
    // Insert a floating picture
    Shape shape = builder.InsertImage(image);
    shape.WrapType = WrapType.None;
    shape.BehindText = true;

    shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
    shape.RelativeVerticalPosition = RelativeVerticalPosition.Page;

    // Calculate image left and top position so it appears in the center of the page
    shape.Left = (builder.PageSetup.PageWidth - shape.Width) / 2;
    shape.Top = (builder.PageSetup.PageHeight - shape.Height) / 2;
}

doc.Save(ArtifactsDir + "DocumentBuilder.InsertWatermark.NetStandard2.doc");
See Also