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);
view raw test.cs hosted with ❤ by GitHub

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);
view raw test.cs hosted with ❤ by GitHub

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:

Loading...

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);
}
view raw test.cs hosted with ❤ by GitHub

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);
}
}
view raw test.cs hosted with ❤ by GitHub

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 TX Text Control .NET for Windows Forms
TXTextControl Namespace
TXTextControl Enumerations Enumerations
ImageInsertionMode Enumeration Enumeration
Determines how an Image can be inserted in the text.
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);
view raw test.cs hosted with ❤ by GitHub

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.