简体   繁体   中英

Insert Image in RichTextBox along with Text C#

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

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.

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();
    }
private static void createImage(RichTextBox item)
{
  var image = new Hashtable(1) { { item, Properties.Resources.yourimage } };
  Clipboard.SetImage((Image)image[item]);
  item.Paste();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM