How to: Insert a Picture

  • 2 minutes to read

The RichEditControl supports the following graphic types:

  • 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)

Use the following API to place an image in the document:

API

Description

DocumentImageCollection.Append

Appends an image.

DocumentImageCollection.Insert
ShapeCollection.InsertPicture

Inserts an image in the specified position.

The code sample belows inserts an image at the document's start.

To insert a picture in line with text, use the DocumentImageCollection.Insert method.

NOTE

A complete sample project is available at https://github.com/DevExpress-Examples/wpf-richedit-document-api-t213968.

DocumentPosition pos = document.Range.Start;
StreamResourceInfo streamResInfo = Application.GetResourceStream(new Uri("beverages.png", UriKind.Relative));
if (streamResInfo != null)
{
    using (Stream s = streamResInfo.Stream)
    {
        document.Images.Insert(pos, DocumentImageSource.FromStream(s));
    }
}

The created DocumentImage object is added to two collections: DevExpress.XtraRichEdit.API.Native.ShapeCollection and DevExpress.XtraRichEdit.API.Native.DocumentImageCollection The object's Shape.TextWrapping property will be set to TextWrappingType.InLineWithText

The code sample below inserts an image in the specified position. The resulting object is added to the ShapeCollection only.

NOTE

A complete sample project is available at https://github.com/DevExpress-Examples/wpf-richedit-document-api-t213968.

document.AppendText("Line One\nLine Two\nLine Three");
StreamResourceInfo streamResInfo = Application.GetResourceStream(new Uri("beverages.png", UriKind.Relative));
if (streamResInfo != null)
{
    using (Stream s = streamResInfo.Stream) 
    {
        Shape myPicture = document.Shapes.InsertPicture(document.CreatePosition(15), 
            DocumentImageSource.FromStream(s));
        myPicture.HorizontalAlignment = ShapeHorizontalAlignment.Center;
    }
}