c# - Add Adorner to Image in WPF RichTextBox when loaded -
i have richtextbox control in application developing using along toolbar create rich text editor. 1 feature have implemented ability user insert image, worth noting @ point output richtextbox rtf.
when user inserts image using following code add image document , add resizeadorner (example here richtextbox resizing adorner) image allows user resize. when user saves , loads document size of image persisted correctly.
private void btninsertimage_onclick(object sender, routedeventargs e) { openfiledialog ofd = new openfiledialog(); ofd.multiselect = false; ofd.checkfileexists = true; ofd.checkpathexists = true; ofd.filter = "image files (*.png;*.jpg;*.jpeg;*.gif;*.bmp)|*.png;*.jpg;*.jpeg;*.gif;*.bmp"; ofd.initialdirectory = environment.getfolderpath(environment.specialfolder.mypictures); ofd.title = "insert image"; if (ofd.showdialog() == true) { bitmapimage bitmap = new bitmapimage(new uri(ofd.filename, urikind.relativeorabsolute)) { cacheoption = bitmapcacheoption.onload }; image image = new image(); image.isenabled = true; image.source = bitmap; image.width = bitmap.width; image.height = bitmap.height; image.loaded += this.imageonloaded; image.stretch = stretch.uniform; inlineuicontainer container = new inlineuicontainer(image, this.rtbeditor.selection.start); paragraph paragraph = new paragraph(container); var doc = this.rtbeditor.document; doc.blocks.add(paragraph); } } private void imageonloaded(object sender, routedeventargs routedeventargs) { var img = sender image; if (img != null) { var al = adornerlayer.getadornerlayer(img); if (al != null) { al.add(new resizingadorner(img)); } } }
the problem , question when document loaded unable figure out how add resizingadorner images in document. using attached property load document contents, code below part loads document:
var stream = new memorystream(encoding.utf8.getbytes(getdocumentxaml(richtextbox))); var doc = new flowdocument(); var range = new textrange(doc.contentstart, doc.contentend); range.load(stream, dataformats.xaml); richtextbox.document = doc;
could how can add resizingadorner images in loaded document please?
so have worked out way can add adorner image in document through selectionchanged event handler , following code
var inline = this.rtbeditor.caretposition.getadjacentelement(logicaldirection.forward) inline; if (inline != null) { this.addadorner(inline.nextinline inlineuicontainer); this.addadorner(inline.previousinline inlineuicontainer); }
and addadorner method
private void addadorner(inlineuicontainer container) { if (container != null) { var image = container.child; if (image != null) { var al = adornerlayer.getadornerlayer(image); if (al != null) { var currentadorners = al.getadorners(image); if (currentadorners != null) { foreach (var adorner in currentadorners) { al.remove(adorner); } } al.add(new resizingadorner(image)); } } } }
i know not elegant way achieve me works wanted post others can choose use if appropriate
Comments
Post a Comment