Skype:TextControlSupport
Orders:877-462-4772
TX Text Control - word processing components.
What is this?Syndicate this content

Inserting images from memory

This source code snippet requires TX Text Control .NET
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 Download [34.31 KB, 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.

  1. Private Sub InsertImageFromMemory(ByVal image As System.Drawing.Image, ByVal _
  2. AsFixedImage As Boolean, ByVal tx As TXTextControl.TextControl)
  3. Dim stream As New System.IO.MemoryStream
  4. Dim buffer() As Byte
  5. Dim RTFString As String
  6. Dim ImageString As New System.Text.StringBuilder
  7.  
  8. image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg)
  9. buffer = stream.ToArray()
  10.  
  11. For Each nbyte As Byte In buffer
  12. ImageString.Append(nbyte.ToString("x2"))
  13. Next
  14.  
  15. RTFString = "{\rtf1" + vbCr
  16.  
  17. If AsFixedImage = True Then RTFString += "{\shp{\*\shpinst\shpfhdr0\ _
  18. shpbxcolumn\shpbypara\shpwr2\shpwrk0\shpfblwtxt0\shplid1025{\sp _
  19. {\sn shapeType}{\sv 75}}{\sp{\sn dxWrapDistLeft}{\sv 0}}{\sp _
  20. {\sn dxWrapDistRight}{\sv 0}}{\sp{\sn pib}{\sv "
  21.  
  22. RTFString += "{\pict\jpegblip\picscalex100\picscaley100" + vbCr
  23. RTFString += ImageString.ToString()
  24. RTFString += "}}"
  25.  
  26. If AsFixedImage = True Then RTFString += "\par}"
  27.  
  28. tx.Selection.Load(RTFString, TXTextControl.StringStreamType.RichTextFormat)
  29. End Sub

This function allows the image to be inserted as a character or fixed anchored at a paragraph. It is called like this:

  1. Dim myImage As System.Drawing.Image = System.Drawing.Image.FromFile("Blue hills.jpg")
  2. 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.

top

Top 10 Bestselling Product Award 2007Top 25 Publisher Product Award 2007Top 10 Bestselling Product Award 2007 in JapanTop 25 Bestselling Product Award 2006Top 25 Bestselling Publisher Award 2006Reader's Choice Award, dot.net magazin, 3rd place