A newer version of this page is available. Switch to the current version.

Pictures

  • 3 minutes to read

Pictures in a document can be inline or floating objects.

NOTE

This document describes inline pictures. For information about floating pictures, refer to the Shapes article.

Inline picture in a document is represented by the DocumentImage object. It occupies the range equivalent to one character, i.e. the DocumentRange.Length of the range is equal to 1.

The DocumentImage.Image property provides access to the previous level of image abstraction, and enables you to get information on image size, resolution and color depth. The OfficeImage object obtained via this property 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).

All images in the document are contained in the DocumentImageCollection accessible using the SubDocument.Images property. Use the DocumentImageCollection.Insert and DocumentImageCollection.Append methods to insert pictures into a document.

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

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 = "http://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);
TIP

The ReadOnlyDocumentImageCollection.Get method is designed to get images within the specified Range. To call the method, use the RichEditDocumentServer.Document.Images.Get notation.

When a document is saved in RTF, inline pictures are saved twice by default: in the native format and as a metafile. Use the RtfDocumentExporterCompatibilityOptions.DuplicateObjectAsMetafile property to modify this behavior.

When a document is saved in HTML format, you can specify a location for storing images as external files, or process them before saving. To accomplish this, handle the RichEditControl.BeforeExport event and specify the HtmlDocumentExporterOptions.UriExportType and IExporterOptions.TargetUri. Moreover, you can implement your own class with the IUriProvider interface and specify it, instead of the default URI provider in the SubDocument.GetHtmlText method.

The Document.HtmlText method uses a special URI provider (the DataStringUriProvider), which converts an image to a base64-encoded representation with the “data:” prefix. Therefore, if you obtain the HTML of a document via the Document.HtmlText method, all images are embedded in the page.