UWP RichEditBox 禁用图像大小调整 [英] UWP RichEditBox disable image resize

查看:25
本文介绍了UWP RichEditBox 禁用图像大小调整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个本机编辑器,在其中我有一个丰富的编辑框,我可以在其中插入图像.我可以从编辑器调整图像大小,如何禁用调整大小.Al那么我怎样才能取回插入的图像.

解决方案

我可以从编辑器调整图像大小,如何禁用调整大小

如果您想保持图像原始大小并插入到RichEditBox,您可以使用 获取图像PixelWidthPixelHeight 值BitmapImage 如下所示.

Windows.Storage.Pickers.FileOpenPicker open = new Windows.Storage.Pickers.FileOpenPicker();open.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;open.FileTypeFilter.Add(".png");Windows.Storage.StorageFile 文件 = await open.PickSingleFileAsync();如果(文件!= null){使用 (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read)){BitmapImage image = new BitmapImage();等待 image.SetSourceAsync(fileStream);Test.Document.Selection.InsertImage(image.PixelWidth, image.PixelHeight, 0, VerticalCharacterAlignment.Baseline, "img", fileStream);}}

<块引用>

还有我怎样才能得到插入的图像

从这个案例回复,您可以从您选择的 rtf 文本中解析图片数据.然后使用正则表达式过滤可用数据.下面是完整的代码,可以直接使用.

private async void GetImage(object sender, RoutedEventArgs e){字符串 rtf = "";Test.Document.Selection.GetText(TextGetOptions.FormatRtf, out rtf);字符串 imageDataHex = "";var r = new Regex(@"pict[\s\S]+?[\r\n](?[\s\S]+)[\r\n]\}\\par",RegexOptions.None);var m = r.Match(rtf);如果(m.成功){imageDataHex = m.Groups["imagedata"].Value;}byte[] imageBuffer = ToBinary(imageDataHex);StorageFile tempfile = await ApplicationData.Current.LocalFolder.CreateFileAsync("temppic.png", CreationCollisionOption.ReplaceExisting);等待 FileIO.WriteBufferAsync(tempfile, imageBuffer.AsBuffer());}public static byte[] ToBinary(string imageDataHex){//这个函数完全取自://http://www.codeproject.com/Articles/27431/Writing-Your-Own-RTF-Converterif (imageDataHex == null){throw new ArgumentNullException("imageDataHex");}int hexDigits = imageDataHex.Length;int dataSize = hexDigits/2;字节[] imageDataBinary = 新字节[数据大小];StringBuilder hex = new StringBuilder(2);int dataPos = 0;for (int i = 0; i 

I'm trying to build a native editor in which i have a rich edit box in which i insert an image. I'm able to resize the image from the editor, how can I disable resizing. Also how can i get the inserted image back.

解决方案

I'm able to resize the image from the editor, how can I disable resizing

If you want to keep image original size and insert to RichEditBox, you could get the image PixelWidth and PixelHeight value with BitmapImage like the follow.

Windows.Storage.Pickers.FileOpenPicker open = new Windows.Storage.Pickers.FileOpenPicker();
open.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;
open.FileTypeFilter.Add(".png");
Windows.Storage.StorageFile file = await open.PickSingleFileAsync();
if (file != null)
{
    using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))

    {
        BitmapImage image = new BitmapImage();
        await image.SetSourceAsync(fileStream);
        Test.Document.Selection.InsertImage(image.PixelWidth, image.PixelHeight, 0, VerticalCharacterAlignment.Baseline, "img", fileStream);
    }
}

Also how can i get the inserted image

Derive from this case reply, you could parse picture data from your selected rtf text. Then use regular expression to filter available data. The follow is a complete code that you could use directly.

private async void GetImage(object sender, RoutedEventArgs e)
{
    string rtf = "";
    Test.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.png", CreationCollisionOption.ReplaceExisting);
    await FileIO.WriteBufferAsync(tempfile, imageBuffer.AsBuffer());
}

public static byte[] ToBinary(string imageDataHex)
{
    //this function taken entirely from:
    // http://www.codeproject.com/Articles/27431/Writing-Your-Own-RTF-Converter
    if (imageDataHex == null)
    {
        throw new ArgumentNullException("imageDataHex");
    }

    int hexDigits = imageDataHex.Length;
    int dataSize = hexDigits / 2;
    byte[] imageDataBinary = new byte[dataSize];

    StringBuilder hex = new StringBuilder(2);

    int dataPos = 0;
    for (int i = 0; i < hexDigits; i++)
    {
        char c = imageDataHex[i];
        if (char.IsWhiteSpace(c))
        {
            continue;
        }
        hex.Append(imageDataHex[i]);
        if (hex.Length == 2)
        {
            imageDataBinary[dataPos] = byte.Parse(hex.ToString(), System.Globalization.NumberStyles.HexNumber);
            dataPos++;
            hex.Remove(0, 2);
        }
    }
    return imageDataBinary;
}

这篇关于UWP RichEditBox 禁用图像大小调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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