Extract Images

This demo uses the DevExpress Word Processing Document API to extract images from a document. You can process the predefined sample file or supply your own document. To do the latter, select Upload a File in the file selection drop-down menu.

Specify whether to extract charts as images in the Extraction Settings panel. Use the Extract Images as ... button to specify the output format, extract images, and download the result.

Select a Document
Sample.docx



using DevExpress.Drawing;
using DevExpress.Office.Services;
using DevExpress.Spreadsheet.Charts;
using DevExpress.XtraRichEdit;
using DevExpress.XtraSpreadsheet.Services;

IReadOnlyList<Stream> ExtractImages(Stream inputStream, bool exportCharts, DXImageFormat imageFormat) {
    OfficeCharts.Instance.ActivateCrossPlatformCharts();
    using var wordProcessor = new RichEditDocumentServer();
    try {
        IList<Stream> imageStreams = new List<Stream>();
        wordProcessor.LoadDocument(inputStream);

        var document = wordProcessor.Document;
        foreach(var shape in document.Shapes) {
            if(shape.Type == DevExpress.XtraRichEdit.API.Native.ShapeType.Picture) {
                Stream imgStream = new MemoryStream();
                shape.PictureFormat.Picture.DXImage.Save(imgStream, imageFormat);
                imageStreams.Add(imgStream);
            }
            else if(exportCharts && shape.Type == DevExpress.XtraRichEdit.API.Native.ShapeType.Chart) {
                Stream imgStream = new MemoryStream();
                ((ChartObject)shape.ChartFormat.Chart).GetImage().DXImage.Save(imgStream, imageFormat);
                imageStreams.Add(imgStream);
            }
        }
        return imageStreams.AsReadOnly();
    }
    catch {
        return Array.Empty<Stream>();
    }
}