none
Get image from rtf,have a problem RRS feed

  • Question

  • The following code can get the picture, but it is not as clear as before.Thank you for pointing out the problem.

    Note:richtextbox.copy() is only useful in app that can read rtf, just like word, wordpad, but can't paste into Excel.

    full test code source pan.baidu.com/s/1kWiDFnh 

     private void btnCopy_Click(object sender, EventArgs e)
        {
            Clipboard.Clear();
            string rtfTxt = richTextBox1.SelectedRtf;
            Match mat = Regex.Match(rtfTxt, @"picw[\d]+");
            mat = Regex.Match(rtfTxt, @"picwgoal[\d]+");
            //width of image
            int width = int.Parse(mat.Value.Replace("picwgoal", "")) / 15;
            mat = Regex.Match(rtfTxt, @"pichgoal[\d]+");
    
            //height of image
            int hight = int.Parse(mat.Value.Replace("pichgoal", "")) / 15;
            string content = rtfTxt.Substring(mat.Index + mat.Length, rtfTxt.IndexOf("}", (mat.Index + mat.Length)) - (mat.Index + mat.Length));
            string text = content.Replace("\r\n", "").Replace(" ", "");
    
            int _Count = text.Length / 2;
            byte[] buffer = new byte[_Count];
            for (int z = 0; z != _Count; z++)
            {
                string _TempText = text[z * 2].ToString() + text[(z * 2) + 1].ToString();
                buffer[z] = Convert.ToByte(_TempText, 16);
            }
            MemoryStream m = new MemoryStream(buffer);
            using (Bitmap bmp = new Bitmap(m, true))
            {
                using (Bitmap bmp1 = new Bitmap(bmp, width, hight))
                //using (Bitmap bmp1 = new Bitmap(bmp))
                {
    
                    //bmp1.Save(@"C:\SL.jpg");
                    Clipboard.SetImage(bmp1);
    
                }
    
            }
    
        }

    Thanks for any reply.


    vb.net

    Saturday, February 10, 2018 5:15 AM

Answers

  • Note:richtextbox.copy() is only useful in app that can read rtf, just like word, wordpad, but can't paste into Excel.

    I tested with Copy() and I can paste it into Excel (Excel 2016 on Windows 10) =>

    Clipboard.Clear();            
    for (int n = 0; (n <= richTextBox1.TextLength); n++)
    {
        richTextBox1.Select(n, 1);
        if ((richTextBox1.SelectionType == RichTextBoxSelectionTypes.Object))
        {                  
            richTextBox1.Copy();
        }
    }

    • Marked as answer by KfCalf Saturday, March 3, 2018 12:13 PM
    Tuesday, February 13, 2018 4:11 PM

All replies


  • Clipboard.Clear();
    richTextBox1.SelectAll();
    richTextBox1.Copy();

    works for me and I can paste it into Excel (Windows 10, Excel 2016)


    • Edited by Castorix31 Saturday, February 10, 2018 9:51 AM
    Saturday, February 10, 2018 9:48 AM
  • Probably the original image bmp is larger than bmp1, and the default downscaling is not satisfactory. Try copying bmp instead of bmp1.

    Or consider some different methods, such as the ResizeImage function: https://social.msdn.microsoft.com/Forums/vstudio/en-US/c2f9b3d6-bbef-4dec-ac98-9ebf3e7d58d0. Try various parameters.


    • Edited by Viorel_MVP Saturday, February 10, 2018 10:39 AM
    Saturday, February 10, 2018 10:39 AM
  • Thanks for reply~

    It doesn't seem to have anything to do with size, even if I set it to the size of the original picture, the picture will be distorted.


    vb.net

    Saturday, February 10, 2018 2:22 PM
  • Another way is to use EM_FORMATRANGE
    Saturday, February 10, 2018 3:09 PM
  • thanks,have any detail about EM_FORMATRANGE?

    vb.net

    Sunday, February 11, 2018 3:10 AM
  • Hello KfCalf,

    For your code, it's correct way that extract image info from RTF string. The main issue is the process of transporting image data to clipboard. When you want to build the bitmap object you are brokening original image encoding. As for a workaround, you just need to pass image data to richtextbox2 directly.

     private void btnTest_Click(object sender, EventArgs e)
            {
                Clipboard.Clear();
                string rtfTxt = richTextBox1.SelectedRtf;
                Match mat = Regex.Match(rtfTxt, @"picw[\d]+");
                mat = Regex.Match(rtfTxt, @"picwgoal[\d]+");
                //width of image
                int width = int.Parse(mat.Value.Replace("picwgoal", "")) / 15;
                mat = Regex.Match(rtfTxt, @"pichgoal[\d]+");
    
                //height of image
                int hight = int.Parse(mat.Value.Replace("pichgoal", "")) / 15;
                string content = rtfTxt.Substring(mat.Index + mat.Length, rtfTxt.IndexOf("}", (mat.Index + mat.Length)) - (mat.Index + mat.Length));
                string text = content.Replace("\r\n", "").Replace(" ", "");
    
                int _Count = text.Length / 2;
                byte[] buffer = new byte[_Count];
                for (int z = 0; z != _Count; z++)
                {
                    string _TempText = text[z * 2].ToString() + text[(z * 2) + 1].ToString();
                    buffer[z] = Convert.ToByte(_TempText, 16);
                }
    
                MemoryStream m = new MemoryStream(buffer);
      
                Clipboard.SetDataObject(Bitmap.FromStream(m, true));
    
            }

    The result

     Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 12, 2018 7:41 AM
    Moderator
  • Thanks for reply~!

    I actually want to copy the images to other programs like excel.

    if use:

      Clipboard.SetDataObject(Bitmap.FromStream(m, true));

    Will don't work~


    vb.net

    Monday, February 12, 2018 9:12 AM
  • Note:richtextbox.copy() is only useful in app that can read rtf, just like word, wordpad, but can't paste into Excel.

    I tested with Copy() and I can paste it into Excel (Excel 2016 on Windows 10) =>

    Clipboard.Clear();            
    for (int n = 0; (n <= richTextBox1.TextLength); n++)
    {
        richTextBox1.Select(n, 1);
        if ((richTextBox1.SelectionType == RichTextBoxSelectionTypes.Object))
        {                  
            richTextBox1.Copy();
        }
    }

    • Marked as answer by KfCalf Saturday, March 3, 2018 12:13 PM
    Tuesday, February 13, 2018 4:11 PM
  • Note:richtextbox.copy() is only useful in app that can read rtf, just like word, wordpad, but can't paste into Excel.

    I tested with Copy() and I can paste it into Excel (Excel 2016 on Windows 10) =>

    Clipboard.Clear();            
    for (int n = 0; (n <= richTextBox1.TextLength); n++)
    {
        richTextBox1.Select(n, 1);
        if ((richTextBox1.SelectionType == RichTextBoxSelectionTypes.Object))
        {                  
            richTextBox1.Copy();
        }
    }

    thanks for try! When i load a rtf to Richtextbox1,and use the code to copy image it can't work. It seems only work when we paste a image to richtextbox.

    Here is test project    https://pan.baidu.com/s/1nxac7ZN

    Note:After richtextbox open a rtf,  the image in rtf can't be copied to excel

                // Create an OpenFileDialog to request a file to open.
                OpenFileDialog openFile1 = new OpenFileDialog();
    
                // Initialize the OpenFileDialog to look for RTF files.
                openFile1.DefaultExt = "*.rtf";
                openFile1.Filter = "RTF Files|*.rtf";
    
                // Determine whether the user selected a file from the OpenFileDialog.
                if (openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    // Load the contents of the file into the RichTextBox.
                    richTextBox1.LoadFile(openFile1.FileName, RichTextBoxStreamType.RichText);
                }


    vb.net

    Wednesday, February 14, 2018 6:01 AM