Beispiel #1
0
        private async void btnChoosePhoto_Click(object sender, RoutedEventArgs e)
        {
            FileOpenPicker openPicker = new FileOpenPicker();

            openPicker.ViewMode = PickerViewMode.Thumbnail;
            openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            openPicker.FileTypeFilter.Add(".jpg");
            openPicker.FileTypeFilter.Add(".jpeg");
            openPicker.FileTypeFilter.Add(".png");
            StorageFile file = await openPicker.PickSingleFileAsync();

            if (file != null)
            {
                var source         = new BitmapImage();
                var imageContainer = new C1InlineUIContainer {
                    Content = source, ContentTemplate = ImageAttach.ImageTemplate
                };
                var stream = await file.OpenAsync(FileAccessMode.Read);

                source.SetSource(stream);
                using (var dataReader = new DataReader(stream))
                {
                    stream.Seek(0);
                    var bytes = new byte[stream.Size];
                    await dataReader.LoadAsync((uint)stream.Size);

                    dataReader.ReadBytes(bytes);
                    ImageAttach.SetStream(source, bytes);
                }
                ImageAttach.SetFormat(source, "image/" + file.FileType.Substring(1));
                using (new DocumentHistoryGroup(RichTextBox.DocumentHistory))
                {
                    RichTextBox.Selection.Delete();
                    RichTextBox.Selection.Start.InsertInline(imageContainer);
                }
            }
            else
            {
            }

            Close();
            RichTextBox.Focus(FocusState.Programmatic);
        }
Beispiel #2
0
        private async void btnPicture_Click(object sender, RoutedEventArgs e)
        {
            FileOpenPicker openPicker = new FileOpenPicker();

            openPicker.ViewMode = PickerViewMode.Thumbnail;
            openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            openPicker.FileTypeFilter.Add(".jpg");
            openPicker.FileTypeFilter.Add(".jpeg");
            openPicker.FileTypeFilter.Add(".png");
            StorageFile file = await openPicker.PickSingleFileAsync();

            if (file != null)
            {
                var source     = new BitmapImage();
                var newElement = new C1InlineUIContainer {
                    Content = source, ContentTemplate = ImageAttach.ImageTemplate
                };
                ExceptionRoutedEventHandler failed = delegate
                {
                    newElement.Remove();
                };
                source.ImageFailed += failed;
                var stream = await file.OpenAsync(FileAccessMode.Read);

                source.SetSource(stream);

                using (var dataReader = new DataReader(stream))
                {
                    stream.Seek(0);
                    var bytes = new byte[stream.Size];
                    await dataReader.LoadAsync((uint)stream.Size);

                    dataReader.ReadBytes(bytes);
                    ImageAttach.SetStream(source, bytes);
                }
                using (new DocumentHistoryGroup(rtb.DocumentHistory))
                {
                    rtb.Selection.Delete();
                    rtb.Selection.Start.InsertInline(newElement);
                }
                rtb.Focus(FocusState.Programmatic);
            }
        }
        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();
        }