Image in TextBox & RichTextBox

| December 25, 2010 | 0 Comments

This article deals with how to show Image in TextBox and RichTextBox control.

I googled for same but found little or was too complex to implement as per my understanding. Well! The noteworthy point is that in either of methods shared here uses alternative solution. There is no such direct way to do. Second thing is that both of these methods are having their disadvantage associated with them (Explained more…).

Image in Textbox

Image in Textbox

Image in RichTextBox

  1. Declare Bitmap object and initialize it with some image file.
  2. Then copy that bitmap into Client’s clipboard by Clipboard.SetDataObject() passing Bitmap object as parameter.
  3. Get the format of Clipboard by DataFormats.Format.
  4. Finally paste that format object in RichTextBox by Paste() passing format object as parameter.

Disadvantage: User is not aware of if his/her system’s clipboard is modified by this program. And sometimes it might make user worried about his system’s behavior.

Image in TextBox

  1. Declare Bitmap object and initialize it with some image file.
  2. Declare PictureBox control. You need to set its location (especially X coordinate based on your TextBox width) for where you want to show image in TextBox control.
  3. And at last add that control in TextBox controls.

Disadvantage: You need to set limit on TextBox MaxLength. Because if user type string more lengthy than textbox width he/she might not be able to see some of the portion of string while typing as it gets hide below PictureBox control.

Code Snippet:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Image_in_TextBox
{
    public partial class Form1 : Form
    {
        private Bitmap Img;
        private PictureBox pictureBox1;

        public Form1()
        {
            InitializeComponent();

            // Retrieving image from Startup Folder (/bin/Debug)
            Img = (Bitmap)Image.FromFile(Application.StartupPath + @"\Images\search.gif");

            pictureBox1 = new PictureBox();
            pictureBox1.Name = "pictureBox1";
            pictureBox1.Size = new System.Drawing.Size(23, 18);
            pictureBox1.Location = new System.Drawing.Point(150, 0);
            pictureBox1.Image = Img;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            #region 1. Image in RichTextBox
            // Copy the bitmap to the clipboard.
            Clipboard.SetDataObject(Img);

            // Get the format for the object type.
            DataFormats.Format format = DataFormats.GetFormat(DataFormats.Bitmap);

            if (richTextBox1.CanPaste(format))
            {
                richTextBox1.Paste(format);

            }
            #endregion

            #region 2. Image in TextBox
            if (pictureBox1 != null)
            {
                this.textBox1.Controls.Add(pictureBox1);
            }
            #endregion
        }
    }
}

Download Code

Category: .NET, C#, Tips

Leave a Reply

%d bloggers like this: