0

I'm trying to insert images in my rich text box in C#, but so far I'm only failing. Miserably.

This is the code that I am using:

Clipboard.SetImage(Image.FromFile(Application.StartupPath + @"\PIC\" + i + ".bmp"));
chat.Paste();

The real problem is I am not able to put both text and image in the textbox. The moment I insert text after copying the image the image disappears. I am unable to find a solution for this

Can anybody help me with this? Please??? Thanks

3 Answers3

2
RichTextBox rtb = new RichTextBox();    
byte[] headerImage = (byte[])(dr["ImageData"]);
                string imageData = string.Empty;
                if (headerImage != null && headerImage.Length > 0)
                {
                    Bitmap bmp = new Bitmap(new MemoryStream(headerImage));
                    MemoryStream ms = new MemoryStream();
                    bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    ms.Position = 0;   
                    imageData = @"{\pict\jpegblip\picw10449\pich3280\picwgoal9924\pichgoal1860\ " + BitConverter.ToString(ms.ToArray()).Replace("-", string.Empty).ToLower() + "}";

                    ms.Dispose();
                }
string finalrtfdata = rtb.Rtf;
                finalrtfdata = finalrtfdata.Replace("&ImageHeader&", imageData);
// finalrtfdata contain your image data along with rtf tags.
0

Try this out,you can paste this into your code and call it: place a picture into your project to embeded resource,and call this method passing the richtextbox.

    private void createImage(Control item)
    {   
        Hashtable image = new Hashtable(1);
        image.Add(item,yourproject.Properties.Resources.yourpicturename);
        Clipboard.SetImage((Image)image[item]);
        ((RichTextBox)item).Paste();
    }
terrybozzio
  • 4,424
  • 1
  • 19
  • 25
  • dont use the dataformats,just paste. Try if(richtextbox.canpaste(df)),it will not enter the if statement. – terrybozzio Sep 28 '13 at 15:44
  • Ok. The issue is that the problem occurs when I add text after it. How do I keep them both. like this http://stackoverflow.com/questions/11086041/append-text-and-image-in-richtextbox – Kanika Lungani Sep 29 '13 at 13:10
  • RichTextBox.AppendText("\n" + yourtext) should do. – terrybozzio Sep 29 '13 at 13:33
  • I did this in a app i played around couple years back about the periodic table where i inserted foreach element i clicked the picture of the element and text description bellow in a richtextbox.,the "\n" is for the text to be placed bellow the picture or else it would start right beside it. – terrybozzio Sep 29 '13 at 13:41
-1
private static void createImage(RichTextBox item)
{
  var image = new Hashtable(1) { { item, Properties.Resources.yourimage } };
  Clipboard.SetImage((Image)image[item]);
  item.Paste();
}
Gabriel Marius Popescu
  • 2,016
  • 2
  • 20
  • 22
  • No reason to convert to Hashtable, resource can be put into clipboard just like that: Clipboard.SetImage( Properties.Resources.yourimage ); – Maciej Apr 08 '19 at 19:45