Paste Image in RichEditBox in Windows 10, Windows Phone 8.1

So to paste Image In Rich Edit Box for Windows 10, Windows Phone 8.1 First Drag and Drop a "RichEditBox". Like this:

  1. <RichEditBox x:Name="rtbox"  
  2.    Width="500"  
  3.    HorizontalAlignment="Center"  
  4.    VerticalAlignment="Center"  
  5.    KeyDown="rtKeydown" />  
Now Double Click the "rtKeydown" event .so to achieve our task we need get the content from Clipboard.and read the image content from DataPackage of clipboard like this:
  1. private async void rtKeydown(object sender, KeyRoutedEventArgs e)  
  2. {  
  3.     if (e.Key == Windows.System.VirtualKey.V && e.Key == Windows.System.VirtualKey.Control)  
  4.     {  
  5.         var dataPackageView = Windows.ApplicationModel.DataTransfer.Clipboard.GetContent();  
  6.         if (dataPackageView.Contains(StandardDataFormats.Bitmap))  
  7.         {  
  8.             IRandomAccessStreamReference imageReceived = null;  
  9.             try  
  10.             {  
  11.                 imageReceived = await dataPackageView.GetBitmapAsync();  
  12.             }  
  13.             catch (Exception ex)  
  14.             {}  
  15.             if (imageReceived != null)  
  16.             {  
  17.                 using(var imageStream = await imageReceived.OpenReadAsync())  
  18.                 {  
  19.                     var bitmapImage = new BitmapImage();  
  20.                     bitmapImage.SetSource(imageStream);  
  21.                     rtbox.Document.Selection.InsertImage((int) bitmapImage.PixelWidth, (int) bitmapImage.PixelHeight, 0, Windows.UI.Text.VerticalCharacterAlignment.Baseline, "Image", imageStream);  
  22.                     e.Handled = true;  
  23.                     Clipboard.Clear();  
  24.                 }  
  25.             }  
  26.         }  
  27.         else  
  28.         {  
  29.             //For Exception  
  30.         }  
  31.     }  
  32. }  
Now Run the App.its completed. output will be like this:

Copying the image from Browser.

Browser

Pasting in RichEditBox.

RichEditBox

Please comment below for any doubts.

Ebook Download
View all
Learn
View all