Products Technologies Demo Docs Blog Support Company

Custom Insert Image Dialog in TX Text Control .NET for WPF

The out-of-the-box image insert dialog that comes with TX Text Control can be used to open supported image types. This article shows how to customize a dialog with additional options.

Custom Insert Image Dialog in TX Text Control .NET for WPF

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.

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(";",
                images.ImportFilters.Split('|')
                    .Where((value, index) => index % 2 == 1)
                    .ToArray());

            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(
                System.Drawing.Image.FromFile(dlg.FileName));

            images.Add(image, -1); // at input position
        }
    }
}

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);

Custom image dialog

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

textControl1.Images.CustomDialog(null, true);

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);

Custom image dialog

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Also See

This post references the following in the documentation:

  • TXTextControl.ImageCollection.Add Method
  • TXTextControl.ImageCollection Class
  • TXTextControl.ImageCollection.ImportFilters Property

WPF

Text Control combines the power of a reporting tool and an easy-to-use WYSIWYG word processor - fully programmable and embeddable in your WPF application. TX Text Control .NET for WPF is a royalty-free, fully programmable rich edit control that offers developers a broad range of word processing features in a reusable component for Visual Studio.

See WPF products

Related Post

ASP.NETWindows FormsWPF

Different Ways to Insert Images

Images can be added to a document from various sources: Physical files, streams or System.Drawing.Images. This article explains how to create images and to add them to a document.