简体   繁体   中英

Images in a RichTextBox (FlowDocument)

I can "import" an image (picking a file) and wrap it in Border in an inlineUIContainer.
- But then, when I save the document, the image disappears (very short file).

OR I can paste it in from clipboard (no Border wrapper).
- Then it does get saved with the document - but I have no control over the display...

What's the correct way to import an Image (from a file) and keep the actual bitmap
in the FlowDocument, so it can be saved with the rest of the RichText ?

EDIT:
My problem seems to be, that the image is represented (in XAML) with a URI
- not the actual bitmap - so HOW to import/embed it correctly ?
This is my import code:

string fileName = openFileDialog.FileName;
BitmapImage bitmap = new BitmapImage(new Uri(fileName, UriKind.Absolute));

Image image = new Image();
image.Source = bitmap;
image.Width = bitmap.Width;
image.Height = bitmap.Height;

Border border = new Border();
border.Background = Brushes.Blue;
border.BorderBrush = Brushes.Red;
border.BorderThickness = new Thickness(10);
border.Margin = new Thickness(10);
border.Padding = new Thickness(10);

border.Child = image;

InlineUIContainer box = new InlineUIContainer(border, rt.CaretPosition);

Try encapsulating the Image in a Viewbox instead of a Border like so:

string fileName = openFileDialog.FileName;
BitmapImage bitmap = new BitmapImage(new Uri(fileName, UriKind.Absolute));

BlockUIContainer blockUI = new BlockUIContainer();
Image img = new Image();
img.Source = bitmap;
Viewbox vb = new Viewbox();
vb.StretchDirection = StretchDirection.DownOnly;
vb.Stretch = Stretch.Uniform;
vb.HorizontalAlignment = HorizontalAlignment.Center;
vb.VerticalAlignment = VerticalAlignment.Center;
vb.Child = img;
blockUI.Child = vb;

rt.Document.Blocks.Add(blockUI);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM