TX Text Control comes with pre-configured dialog boxes to select and insert local images into a document. This article shows how to implement an extension method to use a custom dialog to insert images.

Extension Method

The following extension adds a method CustomDialog to the ImageCollection TX Text Control .NET for WPF
TXTextControl Namespace
ImageCollection Class
An instance of the ImageCollection class contains all images in a Text Control document or part of the document represented through objects of the type Image.

public static class TextControlExtensions
public static void CustomDialog (
this TXTextControl.ImageCollection images,
string preferredFormat = null,
bool showAll = true,
OpenFileDialog openFileDialog = null)
// create a new open file dialog or use the given one
OpenFileDialog dlg = (openFileDialog == null)
? new OpenFileDialog() : openFileDialog;
// retrieve supported filters from Text Control
string sImportFormats = images.ImportFilters;
// add an "All Supported Formats" entry with all extensions
if (showAll) {
var sAllImportFormats = String.Join(";",
.Where((value, index) => index % 2 == 1)
sImportFormats = "All Supported Formats|" +
sAllImportFormats + "|" + sImportFormats;
// set the filters for the dialog
dlg.Filter = sImportFormats;
// select the pre-selected filter
if (preferredFormat != null) {
var saFinalFilters = dlg.Filter.Split('|').Where(
(value, index) => index % 2 == 1).ToArray();
// set the index (0-based)
dlg.FilterIndex = Array.FindIndex(saFinalFilters, m => m == preferredFormat) + 1;
// if file is opened by user, create a new image and insert it
if (dlg.ShowDialog() == true) {
TXTextControl.Image image = new TXTextControl.Image(
images.Add(image, -1); // at input position
view raw custom.cs hosted with ❤ by GitHub

This method accepts 3 parameters:

Parameter< Value Type Value description
preferredFormat String The preferred format that should be pre-selected. Sample: "*.png".
showAll bool Specifies whether an additional "All Supported Formats" option should be added to the list.
openFileDialog OpenFileDialog An OpenFileDialog object that can be pre-configured for additional customization.

Extension Method Usage

The following code opens a custom dialog with the preferred format PNG and without the All Supported Formats option:

textControl1.Images.CustomDialog("*.png", false);
view raw api.cs hosted with ❤ by GitHub

Custom image dialog

The following call opens the dialog with the All Supported Formats option:

textControl1.Images.CustomDialog(null, true);
view raw api.cs hosted with ❤ by GitHub

If you want to change the title, the default directory and other options, you can pass a pre-configured OpenFileDialog to the CustomDialog method:

OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "My custom title";
dlg.InitialDirectory = "c:\\";
textControl1.Images.CustomDialog(null, false, dlg);
view raw api.cs hosted with ❤ by GitHub

Custom image dialog