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.5
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 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 + "Transparent background logo.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.docx");
Examples
Shows how to insert 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);

Shape shape;
using (SKBitmap image = SKBitmap.Decode(ImageDir + "Transparent background logo.png"))
{
    // Insert a floating picture
    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.InsertWatermarkNetStandard2.docx");
See Also