0
votes

I have a RichEditBox in UWP with an image already inserted inside. When the image was inserted it was given a certain width and height (in pixels). Now, after the image has been inserted, I would like to select the image and edit the dimensions. Is there any way to achieve this?

Also, I have seen a similar thread that has an incorrect answer. Please remember this is for WINRT (UWP).

How to get image from RichEditBox

1

1 Answers

1
votes

I would like to select the image and edit the dimensions

Firstly, you need get the selected image RTF text from the RichEditBox. For this, you should use the Selection property of ITextDocument.For example:

Richbox.Document.Selection.GetText(TextGetOptions.FormatRtf, out rtf); 

Secondly, after you got the image RTF text, you need to write a converter to convert the RTF to image. Pictures in RTF spec begin with the \pict control word, and can be in hexadecimal (the default) or binary format. The picture in hexadecimal or binary format follows the picture-destination control words. So that your converter could follow this to use regular expression to extract the image.

For this thread you linked, although it is not specially for UWP app, but it tell the way to extract Image from RTF, you still can reference. I use @kmote code snippet with some changes for a simple testing and can work. Testing code snippet as follows:

string rtf = "";
Richbox.Document.Selection.GetText(TextGetOptions.FormatRtf, out rtf); 
string imageDataHex = "";
var r = new Regex(@"pict[\s\S]+?[\r\n](?<imagedata>[\s\S]+)[\r\n]\}\\par", RegexOptions.None);
var m = r.Match(rtf);
if (m.Success)
{
    imageDataHex = (m.Groups["imagedata"].Value;
}  
byte[] imageBuffer = ToBinary(imageDataHex);
StorageFile tempfile = await ApplicationData.Current.LocalFolder.CreateFileAsync("temppic.jpg");
await FileIO.WriteBufferAsync(tempfile, imageBuffer.AsBuffer());

But all of the answers are just for a guide, you may need to write your own flawless library. There is a third party package RtfPipe provide a library for converting RTF to HTML, you may reference some image convert relative code snippet.

If you meet issues when you wrote the converter you could ask new threads with the details that what you have done.