0

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. Alenter image description hereso how can i get the inserted image back.

Saravana Kumar
  • 341
  • 3
  • 14

1 Answers1

1

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;
}
Nico Zhu
  • 32,367
  • 2
  • 15
  • 36
  • Resizing the image i mean by the attached image. I have edited the question please check into the attachement. – Saravana Kumar Aug 01 '18 at 06:50
  • I have tried insert picture, but it could show the resize point flag in my side. could you told how did you do that? – Nico Zhu Aug 01 '18 at 07:31
  • After adding the image pls double click on that u can see the resize points – Saravana Kumar Aug 01 '18 at 07:55
  • double click the image was selected but resize points did not show ? – Nico Zhu Aug 01 '18 at 07:58
  • OMG. why its behaving weird for me. Its the screen shot i attached. Uri imageUri = new Uri("ms-appx:///Assets/StoreLogo.png"); Windows.Storage.StorageFile imageFile = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(imageUri); using (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); } – Saravana Kumar Aug 01 '18 at 09:37
  • Also i couldnot perform a horizontal scroll on richeditbox. Also please sugggest me idea to add blockquote in a richeditbox. – Saravana Kumar Aug 01 '18 at 10:27
  • for horizontal scroll, you need give RichEditBox Fixed width, and make the value of `TextWrapping` false. – Nico Zhu Aug 02 '18 at 01:51
  • 1
    I have the inverted problem. I want to make it possible to resize images in a `RichEditBox`, but according to this question (https://stackoverflow.com/questions/60086403/how-to-display-resizing-adorner-in-an-ima), it seems that this is not possible. How is "disable resizing" meant in this question? – DasElias Jul 03 '20 at 08:53