Back to Devexpress

DocumentImageCollection Interface

officefileapi-devexpress-dot-xtrarichedit-dot-api-dot-native-8503e347.md

latest10.4 KB
Original Source

DocumentImageCollection Interface

A collection of DocumentImage objects.

Namespace : DevExpress.XtraRichEdit.API.Native

Assembly : DevExpress.RichEdit.v25.2.Core.dll

NuGet Package : DevExpress.RichEdit.Core

Declaration

csharp
[ComVisible(true)]
public interface DocumentImageCollection :
    ReadOnlyDocumentImageCollection,
    ISimpleCollection<DocumentImage>,
    IEnumerable<DocumentImage>,
    IEnumerable,
    ICollection
vb
<ComVisible(True)>
Public Interface DocumentImageCollection
    Inherits ReadOnlyDocumentImageCollection,
             ISimpleCollection(Of DocumentImage),
             IEnumerable(Of 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

csharp
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);
vb
Imports System.IO
Imports System.Reflection
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native
Dim server As New RichEditDocumentServer()
server.LoadDocument("Texts\InlinePictures.rtf", DocumentFormat.Rtf)
Dim doc As Document = server.Document

' Insert an image from a file.
Dim rangeFound As DocumentRange = doc.FindAll("Visual Studio Magazine", SearchOptions.CaseSensitive)(0)
Dim pos As DocumentPosition = 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
Dim imageToInsert As String = "information.png"
Dim a As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
Dim imageStream As Stream = a.GetManifestResourceStream("Resources." & imageToInsert)
doc.Images.Insert(pos, DocumentImageSource.FromStream(imageStream))

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

' Insert a barcode.
Dim barCode As 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
Dim docFooter As SubDocument = 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

csharp
Document document = server.Document;
document.LoadDocument("Documents\\MovieRentals.docx", DocumentFormat.Docx);
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);
}
vb
Dim document As Document = server.Document
document.LoadDocument("Documents\MovieRentals.docx", DocumentFormat.Docx)
Dim myRange As DocumentRange = document.CreateRange(0, 100)

' Obtain all images im the specific range:
Dim images As ReadOnlyDocumentImageCollection = document.Images.Get(myRange)

' Export the retrieved images as png files:
If images.Count > 0 Then
    Dim myImage As DevExpress.Office.Utils.OfficeImage = images(0).Image
    Dim image As System.Drawing.Image = myImage.NativeImage
    Dim imageName As String = String.Format("Image_at_pos_{0}.png", images(0).Range.Start.ToInt())
    image.Save(imageName)
    System.Diagnostics.Process.Start("explorer.exe", "/select," & imageName)
End If

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.

csharp
DocumentRange imageRange = richEditDocumentServer1.Document.Images[0].Range;
richEditDocumentServer1.Document.Delete(imageRange);
vb
Dim imageRange As DocumentRange = richEditDocumentServer1.Document.Images(0).Range
richEditDocumentServer1.Document.Delete(imageRange)

See Also

DocumentImageCollection Members

DocumentImage

DocumentImageSource

Images

DevExpress.XtraRichEdit.API.Native Namespace