private void ShowInsertImageDialog() { var dialog = new C1ImageDialog(); window = new Window(); window.Content = dialog; window.ShowInTaskbar = false; window.WindowStartupLocation = WindowStartupLocation.CenterOwner; window.SizeToContent = SizeToContent.WidthAndHeight; window.WindowStyle = WindowStyle.ToolWindow; var elementSource = HwndSource.FromVisual(rtb) as HwndSource; if (elementSource != null) { int GA_ROOT = 2; IntPtr root = GetAncestor(elementSource.Handle, GA_ROOT); new WindowInteropHelper(window).Owner = root; } window.Closed += delegate { rtb.Focus(); }; dialog.UrlSelected += delegate { window.Close(); if (!string.IsNullOrEmpty(dialog.Url)) { Uri uri = null; try { uri = new Uri(dialog.Url, UriKind.Absolute); } catch (UriFormatException) { try { uri = new Uri("http://" + dialog.Url, UriKind.Absolute); } catch (UriFormatException) { return; } } using (new DocumentHistoryGroup(rtb.DocumentHistory)) { rtb.Selection.Delete(); var source = new BitmapImage(uri); var newElement = new C1InlineUIContainer { Content = source, ContentTemplate = ImageAttach.ImageTemplate }; rtb.Selection.Start.InsertInline(newElement); } } rtb.Focus(); }; dialog.FileSelected += delegate { window.Close(); if (dialog.Stream != null) { var source = new BitmapImage(); var newElement = new C1InlineUIContainer { Content = source, ContentTemplate = ImageAttach.ImageTemplate }; source.BeginInit(); source.StreamSource = new MemoryStream(dialog.Stream); source.EndInit(); ImageAttach.SetStream(source, dialog.Stream); using (new DocumentHistoryGroup(rtb.DocumentHistory)) { rtb.Selection.Delete(); rtb.Selection.Start.InsertInline(newElement); } } rtb.Focus(); }; window.ShowDialog(); }