ShapeHasImage Property

Returns true if the shape has image bytes or links an image.

Namespace:  Aspose.Words.Drawing
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.4
Syntax
public bool HasImage { get; }

Property Value

Type: Boolean
Examples
Shows how to extract images from a document and save them as files.
public void ExtractImagesToFiles()
{
    Document doc = new Document(MyDir + "Images.docx");

    NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);
    int imageIndex = 0;
    foreach (Shape shape in shapes.OfType<Shape>())
    {
        if (shape.HasImage)
        {
            string imageFileName =
                $"File.ExtractImagesToFiles.{imageIndex}{FileFormatUtil.ImageTypeToExtension(shape.ImageData.ImageType)}";
            shape.ImageData.Save(ArtifactsDir + imageFileName);
            imageIndex++;
        }
    }
}
Examples
Shows how to delete all images from a document.
Document doc = new Document(MyDir + "Images.docx");
Assert.AreEqual(10, doc.GetChildNodes(NodeType.Shape, true).Count);

// Here we get all shapes from the document node, but you can do this for any smaller
// node too, for example delete shapes from a single section or a paragraph
NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);

// We cannot delete shape nodes while we enumerate through the collection
// One solution is to add nodes that we want to delete to a temporary array and delete afterwards
ArrayList shapesToDelete = new ArrayList();
foreach (Shape shape in shapes.OfType<Shape>())
{
    // Several shape types can have an image including image shapes and OLE objects
    if (shape.HasImage)
        shapesToDelete.Add(shape);
}

// Now we can delete shapes
foreach (Shape shape in shapesToDelete)
    shape.Remove();

Assert.AreEqual(1, doc.GetChildNodes(NodeType.Shape, true).Count);
doc.Save(ArtifactsDir + "Image.DeleteAllImages.docx");
See Also