ImageData Class

Defines an image for a shape.
Inheritance Hierarchy
SystemObject
  Aspose.Words.DrawingImageData

Namespace:  Aspose.Words.Drawing
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.3
Syntax
public class ImageData

The ImageData type exposes the following members.

Properties
  NameDescription
Public propertyCode exampleBiLevel
Determines whether an image will be displayed in black and white.
Public propertyCode exampleBorders
Gets the collection of borders of the image. Borders only have effect for inline images.
Public propertyCode exampleBrightness
Gets or sets the brightness of the picture. The value for this property must be a number from 0.0 (dimmest) to 1.0 (brightest).
Public propertyCode exampleChromaKey
Defines the color value of the image that will be treated as transparent.
Public propertyCode exampleContrast
Gets or sets the contrast for the specified picture. The value for this property must be a number from 0.0 (the least contrast) to 1.0 (the greatest contrast).
Public propertyCode exampleCropBottom
Defines the fraction of picture removal from the bottom side.
Public propertyCode exampleCropLeft
Defines the fraction of picture removal from the left side.
Public propertyCode exampleCropRight
Defines the fraction of picture removal from the right side.
Public propertyCode exampleCropTop
Defines the fraction of picture removal from the top side.
Public propertyCode exampleGrayScale
Determines whether a picture will display in grayscale mode.
Public propertyCode exampleHasImage
Returns true if the shape has image bytes or links an image.
Public propertyCode exampleImageBytes
Gets or sets the raw bytes of the image stored in the shape.
Public propertyCode exampleImageSize
Gets the information about image size and resolution.
Public propertyCode exampleImageType
Gets the type of the image.
Public propertyCode exampleIsLink
Returns true if the image is linked to the shape (when SourceFullName is specified).
Public propertyCode exampleIsLinkOnly
Returns true if the image is linked and not stored in the document.
Public propertyCode exampleSourceFullName
Gets or sets the path and name of the source file for the linked image.
Public propertyCode exampleTitle
Defines the title of an image.
Methods
  NameDescription
Public methodEquals (Inherited from Object.)
Public methodGetHashCode (Inherited from Object.)
Public methodGetType (Inherited from Object.)
Public methodCode exampleSave(Stream)
Saves the image into the specified stream.
Public methodCode exampleSave(String)
Saves the image into a file.
Public methodCode exampleSetImage(Image)
Sets the image that the shape displays.
Public methodCode exampleSetImage(Stream)
Sets the image that the shape displays.
Public methodCode exampleSetImage(String)
Sets the image that the shape displays.
Public methodCode exampleToByteArray
Returns image bytes for any image regardless whether the image is stored or linked.
Public methodCode exampleToImage
Gets the image stored in the shape as a Image object.
Public methodCode exampleToStream
Creates and returns a stream that contains the image bytes.
Public methodToString (Inherited from Object.)
Remarks

Use the ImageData property to access and modify the image inside a shape. You do not create instances of the ImageData class directly.

An image can be stored inside a shape, linked to external file or both (linked and stored in the document).

Regardless of whether the image is stored inside the shape or linked, you can always access the actual image using the ToByteArray, ToStream, ToImage or Save(String) methods. If the image is stored inside the shape, you can also directly access it using the ImageBytes property.

To store an image inside a shape use the SetImage(String) method. To link an image to a shape, set the SourceFullName property.

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 insert a linked image into a document.
DocumentBuilder builder = new DocumentBuilder();

string imageFileName = ImageDir + "Windows MetaFile.wmf";

builder.Write("Image linked, not stored in the document: ");

Shape linkedOnly = new Shape(builder.Document, ShapeType.Image);
linkedOnly.WrapType = WrapType.Inline;
linkedOnly.ImageData.SourceFullName = imageFileName;

builder.InsertNode(linkedOnly);
builder.Writeln();

builder.Write("Image linked and stored in the document: ");

Shape linkedAndStored = new Shape(builder.Document, ShapeType.Image);
linkedAndStored.WrapType = WrapType.Inline;
linkedAndStored.ImageData.SourceFullName = imageFileName;
linkedAndStored.ImageData.SetImage(imageFileName);

builder.InsertNode(linkedAndStored);
builder.Writeln();

builder.Write("Image stored in the document, but not linked: ");

Shape stored = new Shape(builder.Document, ShapeType.Image);
stored.WrapType = WrapType.Inline;
stored.ImageData.SetImage(imageFileName);

builder.InsertNode(stored);
builder.Writeln();

builder.Document.Save(ArtifactsDir + "Image.CreateLinkedImage.doc");
See Also