How can we copy-paste text and image both from clipboard in uwp C#?

Alumni Comp 16 LAXMI SWAMI 46 Reputation points
2021-07-14T10:29:19.913+00:00

I want to paste a note with the combination of text and image in my chat compose box in the same sequence. I am using dataPackageView to check clipboard content

For Example - I need to copy paste a note from a Microsoft note

114490-image.png

DataPackageView dataPackageView = Clipboard.GetContent();  
if (dataPackageView.Contains(StandardDataFormats.Bitmap))  
{           
  var img = await dataPackageView.GetBitmapAsync();  
}  
else if(dataPackageView.Contains(StandardDataFormats.Text)  
{                   
  string text = await dataPackageView.GetTextAsync();  
}  
Developer technologies | Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Answer accepted by question author
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,871 Reputation points
    2021-07-15T01:56:52.643+00:00

    Hello, Welcome to Micorosoft Q&A,

    How can we copy-paste text and image both from clipboard in uwp C#?

    Please refer this code sample Copy Text scenario. you could use dataPackageView.GetHtmlFormatAsync method to get current content with html string and render it with WebView control.

    if (dataPackageView.Contains(StandardDataFormats.Html))  
                {  
                    this.DisplayResourceMapAsync(dataPackageView);  
      
                    string htmlFormat = null;  
                    try  
                    {  
                        htmlFormat = await dataPackageView.GetHtmlFormatAsync();  
                    }  
                    catch (Exception ex)  
                    {  
                        rootPage.NotifyUser("Error retrieving HTML format from Clipboard: " + ex.Message, NotifyType.ErrorMessage);  
                    }  
      
                    if (htmlFormat != null)  
                    {  
                        string htmlFragment = HtmlFormatHelper.GetStaticFragment(htmlFormat);  
                        OutputHtml.NavigateToString("HTML:<br/ > " + htmlFragment);  
                    }  
                }  
                else  
                {  
                    OutputHtml.NavigateToString("HTML:<br/ > HTML format is not available in clipboard");  
                }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.