Back to Devexpress

DocumentImageSource.FromFile(String) Method

officefileapi-devexpress-dot-xtrarichedit-dot-api-dot-native-dot-documentimagesource-dot-fromfile-x28-system-dot-string-x29.md

latest7.8 KB
Original Source

DocumentImageSource.FromFile(String) Method

Creates an image source object from the specified file.

Namespace : DevExpress.XtraRichEdit.API.Native

Assembly : DevExpress.RichEdit.v25.2.Core.dll

NuGet Package : DevExpress.RichEdit.Core

Declaration

csharp
[ComVisible(false)]
public static DocumentImageSource FromFile(
    string fileName
)
vb
<ComVisible(False)>
Public Shared Function FromFile(
    fileName As String
) As DocumentImageSource

Parameters

NameTypeDescription
fileNameString

A string that contains the name of the file from which to create the image.

|

Returns

TypeDescription
DocumentImageSource

A DocumentImageSource object representing the image in the document.

|

Remarks

The following code sample demonstrates how the FromFile method can be used to insert an image:

csharp
using DevExpress.BarCodes;
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;

using (var wordProcessor = new RichEditDocumentServer()) {
    wordProcessor.LoadDocument("Texts\\Pictures.docx");
    Document doc = wordProcessor.Document;

    // Insert an image from a file.
    DocumentRange rangeFound = doc.FindAll("Visual Studio Magazine", SearchOptions.CaseSensitive)[0];
    DocumentPosition pos = doc.Paragraphs[doc.Paragraphs.Get(rangeFound.End).Index + 2].Range.Start;
    doc.Shapes.InsertPicture(pos, DocumentImageSource.FromFile("Pictures\\ReadersChoice.png"));
}
vb
Imports DevExpress.BarCodes
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native

Using wordProcessor = New RichEditDocumentServer()
  wordProcessor.LoadDocument("Texts\Pictures.docx")
  Dim doc As Document = wordProcessor.Document

  ' Insert an image from a file.
  Dim rangeFound As DocumentRange = doc.FindAll("Visual Studio Magazine", SearchOptions.CaseSensitive)(0)
  Dim pos As DocumentPosition = doc.Paragraphs(doc.Paragraphs.Get(rangeFound.End).Index + 2).Range.Start
  doc.Shapes.InsertPicture(pos, DocumentImageSource.FromFile("Pictures\ReadersChoice.png"))
End Using

Note

When you use theFromFile method to create an image, the method locks the file until the application is closed. This happens because the method internally creates a file stream intended to load the image from the file, which is not disposed of together with the RichEditDocumentServer/RichEditControl instance and stays alive until you close the application.

If you require releasing the file while the program is running, you can use the DocumentImageSource.FromStream method to create an image and close the stream manually after the image is inserted into the document.

The following code snippets (auto-collected from DevExpress Examples) contain references to the FromFile(String) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-richedit-document-api/CS/RichEditAPISample/CodeExamples/InlinePictures.cs#L16

csharp
DocumentPosition pos = document.Range.Start;
document.Images.Insert(pos, DocumentImageSource.FromFile("beverages.png"));
#endregion #ImageFromFile

word-document-api-insert-inline-pictures/CS/Program.cs#L26

csharp
DocumentPosition pos = document.Paragraphs[document.Paragraphs.Get(rangeFound.End).Index + 2].Range.Start;
Shape imageFromFile = document.Shapes.InsertPicture(pos, DocumentImageSource.FromFile("Pictures\\ReadersChoice.png"));
imageFromFile.TextWrapping = TextWrappingType.InLineWithText;

winforms-richedit-tables-simple-example/CS/TablesSimpleExample/Form1.cs#L113

csharp
// Insert the customer photo
document.Images.Insert(table[3, 1].Range.Start, DocumentImageSource.FromFile("photo.png"));

word-document-api-table-examples/CS/Program.cs#L93

csharp
//Insert the customer photo
document.Images.Insert(table[3, 0].Range.Start, DocumentImageSource.FromFile("photo.png"));

winforms-richedit-document-api/VB/RichEditAPISample/CodeExamples/InlinePictures.vb#L15

vb
Dim pos As DevExpress.XtraRichEdit.API.Native.DocumentPosition = document.Range.Start
            document.Images.Insert(pos, DevExpress.XtraRichEdit.API.Native.DocumentImageSource.FromFile("beverages.png"))
#End Region ' #ImageFromFile

word-document-api-insert-inline-pictures/VB/Program.vb#L19

vb
Dim pos As DocumentPosition = document.Paragraphs(document.Paragraphs.Get(rangeFound.End).Index + 2).Range.Start
Dim imageFromFile As Shape = document.Shapes.InsertPicture(pos, DocumentImageSource.FromFile("Pictures\ReadersChoice.png"))
imageFromFile.TextWrapping = TextWrappingType.InLineWithText

winforms-richedit-tables-simple-example/VB/TablesSimpleExample/Form1.vb#L92

vb
' Insert the customer photo
document.Images.Insert(table(3, 1).Range.Start, DocumentImageSource.FromFile("photo.png"))
' Insert the customer info

word-document-api-table-examples/VB/Program.vb#L73

vb
'Insert the customer photo
document.Images.Insert(table(3, 0).Range.Start, DocumentImageSource.FromFile("photo.png"))
'Insert the customer info

See Also

Insert

Get

DocumentImageSource Class

DocumentImageSource Members

DevExpress.XtraRichEdit.API.Native Namespace