Skip to main content

DocumentImageCollection Interface

A collection of DocumentImage objects.

Namespace: DevExpress.XtraRichEdit.API.Native

Assembly: DevExpress.RichEdit.v24.1.Core.dll

NuGet Packages: DevExpress.RichEdit.Core, DevExpress.Win.Navigation

Declaration

[ComVisible(true)]
public interface DocumentImageCollection :
    ReadOnlyDocumentImageCollection,
    ISimpleCollection<DocumentImage>,
    IEnumerable<DocumentImage>,
    IEnumerable,
    ICollection

The following members return DocumentImageCollection objects:

Remarks

Create an Image

All images in the document are contained in two collections: DocumentImageCollection (accessible by the SubDocument.Images property) and ShapeCollection (accessible by the SubDocument.Shapes property). Images imported from an HTML/MHT document are placed into the SubDocument.Images collection only.

Call the DocumentImageCollection.Append or DocumentImageCollection.Insert method to place an inline image in the document. The resulting object is also added to the ShapeCollection.

Note

When you work with documents created in a Microsoft Word version earlier than 2007, its inline pictures are stored in the DocumentImageCollection only.

The following image formats are available:

  • Bitmap (*.bmp, *.dib)
  • JPEG File Interchange Format (*.jpg, *.jpeg)
  • Portable Network Graphics (*.png)
  • Graphics Interchange Format (*.gif)
  • Tagged Image Format (*.tif, *.tiff)
  • Microsoft Enhanced Metafile (*.emf)
  • Windows Metafile (*.wmf)
  • Scalable Vector Graphics (*.svg)

Note

Refer to the Shapes article for information on new pictures and shapes.

This example illustrates how to insert inline pictures in a rich text document. The image can be obtained from a file, from a stream or from a URI.

View Example

using System.IO;
using System.Reflection;
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
RichEditDocumentServer server = new RichEditDocumentServer();

server.LoadDocument("Texts\\InlinePictures.rtf", DocumentFormat.Rtf);
Document doc = server.Document;

// Insert an image from a file.
DocumentRange rangeFound = doc.FindAll("Visual Studio Magazine", SearchOptions.CaseSensitive)[0];
DocumentPosition pos = doc.Paragraphs[doc.Paragraphs.Get(rangeFound.End).Index + 2].Range.Start;
doc.Images.Insert(pos, DocumentImageSource.FromFile("Pictures\\ReadersChoice.png"));

// Insert an image from a stream.
pos = doc.Paragraphs[4].Range.Start;
string imageToInsert = "information.png";
Assembly a = Assembly.GetExecutingAssembly();
Stream imageStream = a.GetManifestResourceStream("InlinePictures.Resources." + imageToInsert);
doc.Images.Insert(pos, DocumentImageSource.FromStream(imageStream));

// Insert an image using its URI.
string imageUri = "http://i.gyazo.com/798a2ed48a3535c6c8add0ea7a4fc4e6.png";
SubDocument docHeader = doc.Sections[0].BeginUpdateHeader();
docHeader.Images.Append(DocumentImageSource.FromUri(imageUri, server));
doc.Sections[0].EndUpdateHeader(docHeader);


// Insert a barcode.
DevExpress.BarCodes.BarCode barCode = new DevExpress.BarCodes.BarCode();
barCode.Symbology = DevExpress.BarCodes.Symbology.QRCode;
barCode.CodeText = "https://www.devexpress.com/";
barCode.CodeBinaryData = System.Text.Encoding.Default.GetBytes(barCode.CodeText);
barCode.Module = 0.5;
SubDocument docFooter = doc.Sections[0].BeginUpdateFooter();
docFooter.Images.Append(barCode.BarCodeImage);
doc.Sections[0].EndUpdateFooter(docFooter);

Obtain Image Information

The DocumentImage.Image property enables you to get information on image size, resolution and color depth. The returned OfficeImage object can be used to get a native .NET Image (the OfficeImage.NativeImage property) or to get image data in a different format (the OfficeImage.GetImageBytes method).

The code sample below retrieves all images in the specific document range and exports them in the PNG format.

View Example

Document document = server.Document;
document.LoadDocument("Documents\\MovieRentals.docx", DocumentFormat.OpenXml);
DocumentRange myRange = document.CreateRange(0, 100);

// Obtain all images im the specific range:
ReadOnlyDocumentImageCollection images = document.Images.Get(myRange);

// Export the retrieved images as png files:
if (images.Count > 0)
{
    DevExpress.Office.Utils.OfficeImage myImage = images[0].Image;
    System.Drawing.Image image = myImage.NativeImage;
    string imageName = String.Format("Image_at_pos_{0}.png", images[0].Range.Start.ToInt());
    image.Save(imageName);
    System.Diagnostics.Process.Start("explorer.exe", "/select," + imageName);
}

Tip

Use the ReadOnlyDocumentImageCollection.Get method to get images from the specified range.

Delete an Image

Clear the range occupied by the image to delete this image from the document. The DocumentImage.Range property returns the related range.

DocumentRange imageRange = richEditDocumentServer1.Document.Images[0].Range;
richEditDocumentServer1.Document.Delete(imageRange);
See Also