question

AlumniComp16LAXMISWAMI-6030 avatar image
0 Votes"
AlumniComp16LAXMISWAMI-6030 asked AlumniComp16LAXMISWAMI-6030 edited

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

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();
 }


windows-uwp
image.png (5.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

NicoZhu-MSFT avatar image
0 Votes"
NicoZhu-MSFT answered AlumniComp16LAXMISWAMI-6030 edited

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.




· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for your answer. It helped but I am still not able to figure out how to maintain the sequence of text and images.

0 Votes 0 ·
NicoZhu-MSFT avatar image NicoZhu-MSFT AlumniComp16LAXMISWAMI-6030 ·

If the clipboard content is html, you should not concern the sequence of text and images, it will render by html structure within WebView.

1 Vote 1 ·

yeah that I just got it but still, I am facing an issue in accessing webview in ViewModel. How can I use this property NavigateToString in ViewModel?

0 Votes 0 ·
Show more comments