Back to Devexpress

IdxRichEditDocumentImageCollection.Append(TGraphic) Method

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

latest4.9 KB
Original Source

IdxRichEditDocumentImageCollection.Append(TGraphic) Method

Adds a new inline image to the end of a subdocument.

Declaration

delphi
function Append(AImage: TGraphic): IdxRichEditDocumentImage;

Parameters

NameTypeDescription
AImageTGraphic

The source container that contains an image in any supported bitmap format.

|

Returns

TypeDescription
IdxRichEditDocumentImage

An inserted inline document image.

Returns nil (in Delphi) or nullptr (in C++Builder) if the source image container (AImage) is empty.

|

Remarks

Call the Append function to add a new inline image to the end of 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 end of a document opened in a TdxRichEditControl:

delphi
uses
  dxGDIPlusClasses, // Declares the TdxSmartImage class
  dxSVGImage; // Declares the TdxSVGImageCodec class
//...
procedure TMyForm.cxButton1Click(Sender: TObject);
var
  ADocument: IdxRichEditDocument;
  AImageContainer: TdxSmartImage;
  AFileName: string;
begin
  ADocument := dxRichEditControl1.Document;
  dxOpenPictureDialog1.Execute(Handle); // Invokes the "Open" dialog for image file selection
  AFileName := dxOpenPictureDialog1.FileName;
  if (AFileName = '') then Exit; // Exits the procedure if a user selects no image
  AImageContainer := TdxSmartImage.Create; // Creates a Smart Image container
  try
    AImageContainer.LoadFromFile(AFileName);
    if (AImageContainer.ImageCodec = TdxSVGImageCodec) then // If the container stores an SVG image
      AImageContainer.ConvertToBitmap; // Rasterizes the vector image
    ADocument.Images.Append(AImageContainer); // Creates a new inline image from the stored bitmap
  finally
    AImageContainer.Free; // Destroys the Smart Image container to prevent memory leaks
  end;
end;
cpp
#include "dxGDIPlusClasses.hpp" // Declares the TdxSmartImage class
#include "dxSVGImage.hpp" // Declares the TdxSVGImageCodec class
//...
void __fastcall TMyForm::cxButton1Click(Sender: TObject)
{
  _di_IdxRichEditDocument ADocument;
  TdxSmartImage *AImageContainer;
  UnicodeString AFileName;
  // ...
  ADocument = dxRichEditControl1->Document;
  dxOpenPictureDialog1->Execute(Handle); // Invokes the "Open" dialog for image file selection
  AFileName = dxOpenPictureDialog1->FileName;
  if (AFileName == "") { return; } // Exits the routine if a user selects no image
  AImageContainer = new TdxSmartImage(); // Creates a Smart Image container
  try
  {
    AImageContainer->LoadFromFile(AFileName);
    if (AImageContainer->ImageCodec == __classid(TdxSVGImageCodec)) // If the container stores an SVG image
      AImageContainer->ConvertToBitmap(); // Rasterizes the vector image
    ADocument->Images->Append(AImageContainer); // Creates a new inline image from the stored bitmap
  }
  __finally
  {
    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.Insert Function

IdxRichEditDocumentImageCollection Interface

IdxRichEditDocumentImageCollection Members

dxRichEdit.NativeApi Unit