locked
about RichEditBox: paste operation support

    Question

  • Hello,

     I got question on RichEditBox:

    1,in my RichEditBox, if user copy text & image, e.g. from a web page, then paste in my RichEditBox, only text can be pasted, the image is losted.

    So how can I response to the paste(shortcut : ctrl+v) operation to paste whole data, includeing both text & image?

    2, how can i get the image which i inserted into the RichEditBox control as below :

    MsgInputBox.Document.Selection.InsertImage(80, 80, 0, Windows.UI.Text.VerticalCharacterAlignment.Baseline, "AlternativeText", bitmapStream);

    Thanks so much!


    programmer

    Wednesday, January 16, 2013 10:05 AM

Answers


  • Hi,

    There is no such built in function in RichEditBox. We need to achieve this in these steps.

    1. Get the content in Clipboard.
    2. Get the uri in the HTML content.
    3. Download these image stream from uri.
    4. Insert these steam into RichEditBox.

    Please follow these codes

                // Add the call back of Clipboard changed
                Windows.ApplicationModel.DataTransfer.Clipboard.ContentChanged += this.onClipboardContentChanged;
    
    
    
    
            private async void onClipboardContentChanged(object sender, object e)
            {
                DataPackageView data = Clipboard.GetContent();
                if (data.Contains(StandardDataFormats.Html))
                {
                    // Get html content
                    string htmlSource = await data.GetHtmlFormatAsync();
    
                    // Get Uri in html content
                    List<Uri> links = new List<Uri>();
                    string regexImgSrc = @"<img[^>]*?src\s*=\s*[""']?([^'"" >]+?)[ '""][^>]*?>";
                    MatchCollection matchesImgSrc = Regex.Matches(htmlSource, regexImgSrc, RegexOptions.IgnoreCase | RegexOptions.Singleline);
                    foreach (Match m in matchesImgSrc)
                    {
                        string href = m.Groups[1].Value;
                        links.Add(new Uri(href));
                    }
    
                   
                    foreach (Uri uri in links)
                    {
                        // Get the stream from uri directly 
                        var httpClient = new HttpClient();
                        var content = await httpClient.GetStreamAsync(uri);
                        var randomAccessStream = new InMemoryRandomAccessStream();
                        var outputStream = randomAccessStream.GetOutputStreamAt(0);
                        await RandomAccessStream.CopyAndCloseAsync(content.AsInputStream(), outputStream);
    
                        // Insert image stream into RichEditBox. You can change the image position based on image size.
                        reb.Document.Selection.InsertImage(80, 80, 0, Windows.UI.Text.VerticalCharacterAlignment.Baseline, "AlternativeText", randomAccessStream);
                    }
    
                }
            }

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Jesse Jiang Monday, January 21, 2013 2:21 AM
    Thursday, January 17, 2013 10:15 AM