Back to Devexpress

IdxRichEditDocumentImageCollection.Insert(IdxRichEditDocumentPosition,TGraphic) Method

vcl-dxrichedit-dot-nativeapi-dot-idxricheditdocumentimagecollection-dot-insert-x28-dxrichedit-dot-nativeapi-dot-idxricheditdocumentposition-vcl-dot-graphics-dot-tgraphic-x29.md

latest5.0 KB
Original Source

IdxRichEditDocumentImageCollection.Insert(IdxRichEditDocumentPosition,TGraphic) Method

Inserts a new inline image at the specified position in a subdocument.

Declaration

delphi
function Insert(const APos: IdxRichEditDocumentPosition; AImage: TGraphic): IdxRichEditDocumentImage;

Parameters

NameTypeDescription
APosIdxRichEditDocumentPosition

The target document position.

| | AImage | TGraphic |

The source image container.

|

Returns

TypeDescription
IdxRichEditDocumentImage

The created inline image.

|

Remarks

Call the Insert function to add a new inline image at any position in the parent subdocument.

Code Example: Select and Insert any Image into a Document

The following code example allows users to select an image in any format and insert the image at the current caret position in a document opened in a TdxRichEditControl:

delphi
uses
..., dxSVGImage; // This unit contains the TdxSVGImageCodec class declaration
//...
procedure TMyForm.cxButton1Click(Sender: TObject);
var
  ADocument: IdxRichEditDocument;
  AImageContainer: TdxSmartImage;
  AFileName: string;
begin
  ADocument := dxRichEditControl1.Document;
  OpenPictureDialog1.Execute(Handle); // Invokes the "Open" dialog to allow a user to select the source image file
  AFileName := OpenPictureDialog1.FileName;
  if (AFileName = '') then Exit; // Exits the procedure if a user selects no image
  AImageContainer := TdxSmartImage.Create; // Creates a Smart Image container
  AImageContainer.LoadFromFile(AFileName);
  if (AImageContainer.ImageCodec = TdxSVGImageCodec) then // If the container has loaded an SVG image
    AImageContainer.ConvertToBitmap; // Rasterizes the vector image
  ADocument.Images.Insert(ADocument.CaretPosition, AImageContainer); // Creates a new inline image from the stored bitmap and inserts it at the caret position within the opened document
  AImageContainer.Free; // Destroys the Smart Image container to prevent memory leaks
end;
cpp
#include "dxSVGImage.hpp" // This header contains the TdxSVGImageCodec class declaration
//...
void __fastcall TMyForm::cxButton1Click(Sender: TObject)
{
  _di_IdxRichEditDocument ADocument;
  TdxSmartImage *AImageContainer;
  UnicodeString AFileName;
//...
  ADocument = dxRichEditControl1->Document;
  OpenPictureDialog1->Execute(Handle); // Invokes the "Open" dialog to allow a user to select the source image file
  AFileName = OpenPictureDialog1->FileName;
  if (AFileName == "") { return; } // Exits the routine if a user selects no image
  AImageContainer = new TdxSmartImage(); // Creates a Smart Image container
  AImageContainer->LoadFromFile(AFileName);
  if (AImageContainer->ImageCodec == __classid(TdxSVGImageCodec)) // If the container has loaded an SVG image
    AImageContainer->ConvertToBitmap(); // Rasterizes the vector image
  ADocument->Images->Insert(ADocument->CaretPosition, AImageContainer); // Creates a new inline image from the stored bitmap and inserts it at the caret position within the opened document
  delete AImageContainer; // Destroys the Smart Image container to prevent memory leaks
}

Limitations

An inline document image’s Office Image container does not support vector images to ensure compatibility with popular document formats. Ensure that the source image container (a TdxSmartImage or TdxSmartGlyph instance) does not contain an SVG image. Otherwise, an exception occurs.

Tip

You can call the image container’s ConvertToBitmap procedure to rasterize the stored image as demonstrated in the code example in the current help topic.

See Also

IdxRichEditDocumentImageCollection.Append Function

IdxRichEditDocumentImageCollection Interface

IdxRichEditDocumentImageCollection Members

dxRichEdit.NativeApi Unit