> ## Documentation Index
> Fetch the complete documentation index at: https://docs.abbyy.com/llms.txt
> Use this file to discover all available pages before exploring further.

# AddImageFileFromMemory Method of the FRDocument Object

This method opens an image file from the global memory, where it was previously loaded by the user, and adds the pages corresponding to the opened file to the document.

## Syntax

### C++

Linux and macOS

```cpp theme={null}
HRESULT  AddImageFileFromMemory(
  void*                   DataPtr,
  __int64                 DataSize,
  IImagePasswordCallback* Callback,
  IPrepareImageMode*      PrepareMode,
  IIntsCollection*        PageIndices,
  BSTR                    FileName
);
```

Windows

```cpp theme={null}
HRESULT  AddImageFileFromMemory(
  __int64                 HGlobal,
  IImagePasswordCallback* Callback,
  IPrepareImageMode*      PrepareMode,
  IIntsCollection*        PageIndices,
  BSTR                    FileName
);
```

### C\#

```csharp theme={null}
void AddImageFileFromMemory(
  Int64                  HGlobal,
  IImagePasswordCallback Callback,
  IPrepareImageMode       PrepareMode,
  IIntsCollection         PageIndices,
  string                 FileName
);
```

### Visual Basic .NET

```vb theme={null}
Sub AddImageFileFromMemory( _
  HGlobal As Int64, _
  Callback As IImagePasswordCallback, _
  [PrepareMode As IPrepareImageMode = Nothing], _
  [PageIndices As IIntsCollection = Nothing], _
  [FileName As String = "0"] _
)
```

## Parameters

DataPtr

\[in] Linux: Specifies the address of the memory block which contains the image file.

DataSize

\[in] Linux: Specifies the size of the image loaded into memory.

HGlobal

\[in] macOS and Windows: Specifies the HGLOBAL handle of the memory block which contains the image file. The handle is passed as [\_\_int64](/fine-reader/engine/guided-tour/advanced-techniques/programming-aspects/working-with-properties). This handle should be valid.

Callback

\[in] This variable refers to the interface of the user-implemented object of the type [ImagePasswordCallback](/fine-reader/engine/api-reference/image-related-objects/iimagepasswordcallback) which is used to handle possible password requests for accessing images in PDF format. This parameter is optional and may be 0, in which case password-protected files cannot be processed.

PrepareMode

\[in] Refers to the [PrepareImageMode](/fine-reader/engine/api-reference/image-related-objects/prepareimagemode) object which specifies how an image will be preprocessed during opening. This parameter is optional and may be 0, in which case either the default parameters are used, or, if a [profile](/fine-reader/engine/guided-tour/advanced-techniques/working-with-profiles) has been loaded, the parameters set by this profile are used.

PageIndices

\[in] This parameter refers to the [IntsCollection](/fine-reader/engine/api-reference/supplementary-objects-and-methods/intscollection) object which specifies the indices of the pages which have to be added to the document. Note that repeated indices are not allowed. To add the same page several times, please call this method several times.<br />This parameter is optional and may be 0, in which case all the pages of the opened file will be added to the document.

FileName

\[in] Specifies the name of the image file. This information is used to define the format of the file, if not explicitly specified, and in error messages. This parameter is optional and may be 0.

For opening the office formats (Linux and Windows only), pass the name of the document with its extension.

## Return values

This method has no specific return values. It returns the [standard return values of ABBYY FineReader Engine functions](/fine-reader/engine/api-reference/return-codes).

## Remarks

* Files in SVG format cannot be opened from memory.

* When opening a PDF Portfolio file using this method, its cover is always included in the output file.

* This method may report events to the listeners attached to the IConnectionPointContainer interface of the [FRDocument](/fine-reader/engine/api-reference/document-related-objects/frdocument) object.

* For Linux and Windows:
  * If your license includes the [Office Formats Opening](/fine-reader/engine/licensing/modules#officeformatsopening) module, you may also use this method to open digital documents of [supported formats](/fine-reader/engine/specifications/supported-digital-document-input-formats).
  * This method does not work if the Engine is loaded as an out-of-process server in Linux or if the object is created using the [OutprocLoader](/fine-reader/engine/api-reference/engine-loaders/outprocloader) object in Windows. Consider using [AddImageFileFromStream](/fine-reader/engine/api-reference/document-related-objects/frdocument/addimagefilefromstream-method) (Win) method in this case.
  * Depending on the value of the [IEngine::MultiProcessingParams](/fine-reader/engine/api-reference/engine-object-iengine-interface/properties#multiprocessingparams) property, ABBYY FineReader Engine can distribute opening of multi-page documents to CPU cores.

* For Windows, this method does not support WIC-compatible files opening.

## Samples

<Accordion title="C# code">
  ```csharp theme={null}
  // This sample shows how an image file can be loaded to memory
  // and then added for processing to FineReader Engine
  ...
  // In this code sample we use the System.IO namespace for operation with a file in memory
  using System.IO;
  // and the System.Runtime.InteropServices namespace for working with blocks in memory
  using System.Runtime.InteropServices;
  ...
  // We assume that we have already created the Engine object
  // Create document
  FREngine.IFRDocument document = engine.CreateFRDocument();
  // Source image
  string imagePath = "D:\\Demo.tif";
  FileInfo file = new FileInfo(imagePath);
  // Length of the source image
  long len = file.Length;
  // Open the image
  BinaryReader br = new BinaryReader(File.Open(imagePath, FileMode.Open));
  // Read the specified number of bytes from the current stream into a byte array
  byte[] byteArray = br.ReadBytes((int)len);
  // Allocate a block of memory
  IntPtr handle = Marshal.AllocHGlobal((int)len);
  // Copy data from an unmanaged memory pointer to a managed 32-bit signed integer array
  Marshal.Copy(byteArray, 0, handle, (int)len);
  // Add image from memory to document
  document.AddImageFileFromMemory((Int64)handle, null, null, null, "Demo.tif");
  // Free memory previously allocated from the unmanaged memory of the process
  Marshal.FreeHGlobal(handle);
  // Recognize document
  document.Process(null);
  ```
</Accordion>

## See also

[FRDocument](/fine-reader/engine/api-reference/document-related-objects/frdocument)
