| Skype: | TextControlSupport | |
| Orders: | 877-462-4772 |

| Author: | TX Text Control Support Department |
| Language: | Visual Basic .NET |
| Version: | 1.0 |
| Released: | May 02, 2006 |
| Last modified: | January 11, 2008 |
| Requirements: | TX Text Control .NET with Visual Basic .NET |
| Download code: | tx_sample_images_mem.zip |
TX Text Control offers various functions to insert images from physical files in many supported formats. Sometimes, however, it is necessary that images are inserted from memory.
The Microsoft .NET Framework ships with many options to import, manipulate and export images. This sample illustrates how to insert a System.Drawing.Image into the current document of TX Text Control.
The secret here, is actually quite simple: We use the RTF format to load the image. The System.Drawing.Image is saved as a JPEG, converted into a hex string and embedded into an RTF string that is valid according to the RTF specifications. This document can be loaded using the Selection.Load method.
Private Sub InsertImageFromMemory(ByVal image As System.Drawing.Image, ByVal _ AsFixedImage As Boolean, ByVal tx As TXTextControl.TextControl) Dim stream As New System.IO.MemoryStream Dim buffer() As Byte Dim RTFString As String Dim ImageString As New System.Text.StringBuilder image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg) buffer = stream.ToArray() For Each nbyte As Byte In buffer ImageString.Append(nbyte.ToString("x2")) Next RTFString = "{\rtf1" + vbCr If AsFixedImage = True Then RTFString += "{\shp{\*\shpinst\shpfhdr0\ _ shpbxcolumn\shpbypara\shpwr2\shpwrk0\shpfblwtxt0\shplid1025{\sp _ {\sn shapeType}{\sv 75}}{\sp{\sn dxWrapDistLeft}{\sv 0}}{\sp _ {\sn dxWrapDistRight}{\sv 0}}{\sp{\sn pib}{\sv " RTFString += "{\pict\jpegblip\picscalex100\picscaley100" + vbCr RTFString += ImageString.ToString() RTFString += "}}" If AsFixedImage = True Then RTFString += "\par}" tx.Selection.Load(RTFString, TXTextControl.StringStreamType.RichTextFormat) End Sub
This function allows the image to be inserted as a character or fixed anchored at a paragraph. It is called like this:
Dim myImage As System.Drawing.Image = System.Drawing.Image.FromFile("Blue hills.jpg") InsertImageFromMemory(myImage, False, TextControl1)
Using this technique, it is possible to import image formats that are not directly supported by TX Text Control.
You can download the fully functional sample project for Visual Basic .NET to test this sample application for yourself. The minimum requirements for this sample application are TX Text Control .NET trial version and Visual Studio .NET 2003.