1

I tried to insert image into RichTextBox by clicking the button. I referred this post - How can I insert an image into a RichTextBox? and made the source code

private void button2_Click(object sender, EventArgs e)
{
    OpenFileDialog f = new OpenFileDialog();
    f.Filter = "BMP (*.bmp)|*.bmp|JPEG (*.jpg, *.jpeg, *.jpe, *.jfif)|*.jpg;*.jpeg;*.jpe;*.jfif|GIF (*.gif)|*.gif|PNG (*.png)|*.png|All files|*.bmp;*.jpg;*.jpeg;*.jpe;*.jfif;*.gif;*.png";

    if (f.ShowDialog() == DialogResult.OK)
    {
        // Get type of the file

        string typeOfFile = "";
        Stack<char> typeParse = new Stack<char>();
        int pointer = f.FileName.Length - 1
        while (pointer > 0 && f.FileName[pointer] != '.')
            typeParse.Push(f.FileName[pointer--]);
        while (typeParse.Count > 0)
            typeOfFile += typeParse.Pop();
        typeOfFile = typeOfFile.ToLower();

        // Convert Image to byte[]

        Image image = Image.FromFile(f.FileName);
        MemoryStream ms = new MemoryStream();
        image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

        // Insert the image data in RichTextBox which is converted to hex from byte[]
        // by using BitConverter.ToString(ms.ToArray();

        StringBuilder imageRTF = new StringBuilder();
        imageRTF.Append(@"{\pict\" + typeOfFile + @"blip\picw" + image.Width + @"\pich" + image.Height);
        imageRTF.Append(@"\picwgoal" + image.Width + @"\pichgoal" + image.Height);
        imageRTF.Append(@" " + BitConverter.ToString(ms.ToArray()) + @"}");
        string rtf1 = richTextBox1.Rtf.Trim().TrimEnd('}');
        string rtf2 = imageRTF.ToString();
        richTextBox1.Rtf = rtf1 + rtf2;
    }
}

but when I executed and opened png file, only one line break was inserted in RichTextBox.. What is wrong??

Y X
  • 51
  • 6
  • My guess would be this: Your `richTextBox1.Rtf` has the following blank `Rtf` code - `{\rtf1}` - with the follwing line `string rtf1 = richTextBox1.Rtf.Trim().TrimEnd('}');` you are trimming the last `}` which results in only `{\rtf1` - closing `}` is missing now you only add your `{\pict…}` which results in `{\rtf1{\pict…}` still the closing tag for `rtf1` is missing – Rand Random Nov 21 '18 at 14:13
  • 1
    You could also use the ClipBoard to Copy/Paste an Image into the RTB. Maybe you'll find it easier (and you don't really need to know how RTF works). – Jimi Nov 21 '18 at 14:30
  • I modified the code after seeing your comment. I deleted 3 line below the code (string rtf1, rtf2, richTextBox1.Rtf) and added `richTextBox1.Rtf = richTextBox1.Rtf + imageRTF.ToString();` but this still isn't working... did i wrong?? – Y X Nov 21 '18 at 14:40
  • thank you for good information! i'll try to use the clipboard after fixing this bug – Y X Nov 21 '18 at 14:42
  • 1
    Clipboard is the most commonly used way but beware: It will always be a cludge and a rather volatile one as basically all things one would want are missing, e.g. a frame/textframe you could move around.. – TaW Nov 21 '18 at 15:02

0 Answers0