Products Technologies Demo Docs Blog Support Company

Various Ways of Inserting Images into TX Text Control

TX Text Control provides various ways of inserting images into the document. This article shows different approaches to add images to a document from memory, from a file or from a URL.

Various Ways of Inserting Images into TX Text Control

TX Text Control provides various ways to insert images into a document. This article shows how to insert an image from a file, from memory, from a .NET Image object and how to insert an image from a URL.

Inserting an Image from a File

Inserting an image from a file is the most common way to add images to a document. The following code shows how to insert an image from a file into a document:

string imagePath = "Images/signature1.jpg";

TXTextControl.Image myImage = new TXTextControl.Image() { 
        FileName = imagePath };

textControl1.Images.Add(myImage, -1);

The image filter is automatically detected by TX Text Control. If the file extension is not supported, the image is not inserted and an exception is thrown.

Inserting a .NET System.Drawing.Image

TX Text Control supports inserting images from .NET System.Drawing.Image objects. The following code shows how to insert an image from a .NET Image object:

string imagePath = "Images/signature1.jpg";
System.Drawing.Image img = System.Drawing.Image.FromFile(imagePath);

TXTextControl.Image myImage = new TXTextControl.Image(img);

textControl1.Images.Add(myImage, -1);

The constructor of the Image class accepts a .NET Image object as a parameter. The image is inserted with the original size and resolution.

Adding Images from MemoryStream

Images can be inserted from a MemoryStream object. The following code shows how to insert an image from a MemoryStream:

Images from a Byte Array

Images can be inserted from a byte array. The following code shows how to insert an image from a byte array:

string imagePath = "Images/signature1.jpg";

byte[] bytes = File.ReadAllBytes(imagePath);

using (MemoryStream ms = new MemoryStream(
          bytes, 0, bytes.Length, writable: false, publiclyVisible: true))
{
        // create image object
        TXTextControl.Image myImage = new TXTextControl.Image(ms);
        textControl1.Images.Add(myImage, -1);
}

The writable parameter is set to false, indicating that the stream cannot be written to. publiclyVisible is set to true, meaning the memory stream's buffer can be accessed by other threads safely which is required for the Image class to load the image.

Inserting Images from URLs

Images can be inserted from URLs. Therefore, the image is downloaded and inserted into the document. The following code shows how to insert an image from a URL:

string url = "https://www.textcontrol.com/img/corporate_id/tx_logo.svg";

using (WebClient client = new WebClient())
{
        byte[] bytes = client.DownloadData(url);

        using (MemoryStream ms = new MemoryStream(
                  bytes, 0, bytes.Length, writable: false, publiclyVisible: true))
        {
                // create image object
                TXTextControl.Image myImage = new TXTextControl.Image(ms);
                textControl1.Images.Add(myImage, -1);
        }
}

Image Positioning

Images, like all other FrameBase objects in TX Text Control, can be inserted inline at a character position, anchored to a paragraph, or inserted as a fixed object on a specific page. All of the above examples insert the image at a text position and treat the image inline as a character. The following modes of insertion are available:

  • Inline with text
  • Anchored to a paragraph
  • Fixed on a page

The following InsertionModes are supported:

Member Description
AsCharacter The image is inserted in the text as a single character.
DisplaceCompleteLines The image is inserted at a certain geometrical location. The text stops at the top and continues at the bottom of the image.
DisplaceText The image is inserted at a certain geometrical location. The text flows around the image and empty areas at the left and right side are filled.
AboveTheText The image is inserted at a certain geometrical location above the text. This means that the image overwrites the text.
BelowTheText The image is inserted at a certain geometrical location below the text. This means that the text overwrites the image.
MoveWithText The image is connected with a paragraph and moved with the text.
FixedOnPage The image is fixed positioned on a page.

The values of the ImageInsertionMode enumeration can be combined. The possible combinations are:

  • DisplaceCompleteLines | MoveWithText
  • DisplaceCompleteLines | FixedOnPage
  • DisplaceText | MoveWithText
  • DisplaceText | FixedOnPage
  • AboveTheText | MoveWithText
  • AboveTheText | FixedOnPage
  • BelowTheText | MoveWithText
  • BelowTheText | FixedOnPage
  • AsCharacter

Anchored to Paragraphs

For example, the following code inserts an image, anchoring to the paragraph at the current input position with a 500-twips bidirectional position offset:

TXTextControl.Image image = new TXTextControl.Image("image.png", 4);

textControl1.Images.Add(image, new Point(500,500), -1, 
  TXTextControl.ImageInsertionMode.MoveWithText |
  TXTextControl.ImageInsertionMode.DisplaceText);

The location is specified in twips where the image should be inserted. This is a position relative to the top left corner of either a page or a paragraph.

Image location

Conclusion

This article showed how to insert images from various sources into a document using TX Text Control .NET for Windows Forms. The Image class provides constructors to insert images from files, .NET Image objects, and MemoryStream objects. Images can also be inserted from URLs. The ImageInsertionMode enumeration provides various ways to position images in a document.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Related Posts

ASP.NETWindows FormsImages

Selecting and Formatting TableCells in TX Text Control

This article shows how to select and format table cells in TX Text Control .NET for Windows Forms and TX Text Control .NET Server. The sample code shows how to select a table cell range and how to…


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.


ASP.NETWindows FormsWPF

TX Text Control 33.0 SP3 is Now Available: What's New in the Latest Version

TX Text Control 33.0 Service Pack 3 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…


ASP.NETWindows FormsWPF

TX Text Control 33.0 SP2 is Now Available: What's New in the Latest Version

TX Text Control 33.0 Service Pack 2 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…


ASP.NETWindows FormsWPF

Document Lifecycle Optimization: Leveraging TX Text Control's Internal Format

Maintaining the integrity and functionality of documents throughout their lifecycle is paramount. TX Text Control provides a robust ecosystem that focuses on preserving documents in their internal…