DocumentBuilderInsertImage Method (Byte)
Inserts an image from a byte array into the document. The image is inserted inline and at 100% scale.

Namespace: Aspose.Words
Assembly: Aspose.Words (in Aspose.Words.dll) Version: 19.10
Syntax
public Shape InsertImage(
	byte[] imageBytes
)

Parameters

imageBytes
Type: SystemByte
The byte array that contains the image.

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 different solutions of how to import an image into a document from a byte array.
[C#]

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

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

using (MemoryStream ms = new MemoryStream())
{
    image.Save(ms, ImageFormat.Png);
    byte[] imageByteArray = ms.ToArray();

    builder.Writeln("\nInserted image from byte array: ");
    builder.InsertImage(imageByteArray);

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

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

doc.Save(ArtifactsDir + "InsertImageFromByteArray.docx");
Examples
Shows different solutions of how to import an image into a document from a byte array (.NetStandard 2.0).
[C#]

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

using (SKBitmap bitmap = SKBitmap.Decode(ImageDir + "Aspose.Words.gif"))
{
    using (SKImage image = SKImage.FromBitmap(bitmap))
    {
        using (SKData data = image.Encode()) // Encode the image (defaults to PNG)
        {
            byte[] imageByteArray = data.ToArray();

            builder.Writeln("\nInserted image from byte array: ");
            builder.InsertImage(imageByteArray);

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

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

doc.Save(ArtifactsDir + "InsertImageFromByteArray.NetStandard2.docx");
See Also