如何在 UWP 中获取 RichEditBox 中已有的图像 [英] How to get the image already inside a RichEditBox in UWP

查看:29
本文介绍了如何在 UWP 中获取 RichEditBox 中已有的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UWP 中有一个 RichEditBox,里面已经插入了一个图像.当插入图像时,它被赋予了一定的宽度和高度(以像素为单位).现在,插入图像后,我想选择图像并编辑尺寸.有什么办法可以做到这一点吗?

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?

另外,我看到了一个类似的帖子,但答案不正确.请记住,这是针对 WINRT (UWP) 的.

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

如何从 RichEditBox 获取图片

推荐答案

我想选择图片并编辑尺寸

I would like to select the image and edit the dimensions

首先,您需要从 RichEditBox 中获取选定的图像 RTF 文本.为此,您应该使用 Selection 属性"nofollow noreferrer">ITextDocument.例如:

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

其次,在你得到图像RTF文本后,你需要编写一个转换器将RTF转换成图像.RTF 规范中的 图片\pict 控制字开头, 并且可以是十六进制(默认)或二进制格式.十六进制或二进制格式的图片跟在图片目标控制字之后.这样您的转换器就可以按照此使用正则表达式来提取图像.

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.

对于 这个线程 你链接了,虽然它不是专门用于 UWP应用程序,但它告诉了从RTF中提取图像的方式,您仍然可以参考.我使用@kmote 代码片段进行了一些更改以进行简单的测试并且可以工作.测试代码片段如下:

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

但所有答案仅供参考,您可能需要编写自己的完美库.有一个第三方包 RtfPipe 提供了一个将 RTF 转换为 HTML 的库,你可以参考一些图像转换相关代码片段.

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.

这篇关于如何在 UWP 中获取 RichEditBox 中已有的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆