【问题标题】:UWP RichEditBox disable image resizeUWP RichEditBox 禁用图像调整大小
【发布时间】:2019-01-07 15:44:42
【问题描述】:

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

【问题讨论】:

  • 你用InsertImage api插入图片吗?
  • 是 Document.Selection.InsertImage
  • @NicoZhu-MSFT 另外,对于比屏幕宽度更宽的表格,富编辑框没有水平滚动功能。
  • 禁用图像调整大小意味着保留原始大小插入到 RichEditBox ?我说的对吗?

标签: uwp richeditbox


【解决方案1】:

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

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

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

另外我怎样才能得到插入的图像

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

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

【讨论】:

  • 调整图像大小我的意思是附加图像。我已经编辑了问题,请查看附件。
  • 我试过插入图片,但它可能会在我身边显示调整大小的点标志。你能说说你是怎么做到的吗?
  • 添加图片后双击可以看到调整大小的点
  • 双击图像被选中但调整大小点没有显示?
  • 天啊。为什么它对我来说表现得很奇怪。它是我附上的屏幕截图。 Uri imageUri = new Uri("ms-appx:///Assets/StoreLogo.png"); Windows.Storage.StorageFile imageFile = 等待 Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(imageUri);使用 (Windows.Storage.Streams.IRandomAccessStream ras = await imageFile.OpenAsync(FileAccessMode.Read)) { Editor.Document.Selection.InsertImage(15, 15, 0, Windows.UI.Text.VerticalCharacterAlignment.Baseline, "Local_Image_ms-appx :///Assets/StoreLogo.png", ras); }
猜你喜欢
  • 2020-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-22
  • 1970-01-01
  • 2018-12-01
  • 2020-05-10
相关资源
最近更新 更多