コード例 #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);
        }
コード例 #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);
            }
        }
コード例 #3
0
        private void btnFromWeb_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(txtUrl.Text))
            {
                return;
            }

            Uri uri = null;

            try
            {
                uri = new Uri(txtUrl.Text, UriKind.Absolute);
            }
            catch (FormatException)
            {
                try
                {
                    uri = new Uri("http://" + txtUrl.Text, UriKind.Absolute);
                }
                catch (FormatException)
                {
                    MessageDialog dialog = new MessageDialog(Strings.UnValidUrlMessage, Strings.Error);
                    dialog.ShowAsync();
                    return;
                }
            }

            using (new DocumentHistoryGroup(RichTextBox.DocumentHistory))
            {
                RichTextBox.Selection.Delete();
                BitmapImage         source         = new BitmapImage(uri);
                C1InlineUIContainer imageContainer = new C1InlineUIContainer();
                imageContainer.Content         = source;
                imageContainer.ContentTemplate = ImageAttach.ImageTemplate;
                RichTextBox.Selection.Start.InsertInline(imageContainer);
            }

            Close();
            RichTextBox.Focus(FocusState.Programmatic);
        }
コード例 #4
0
        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();
        }
コード例 #5
0
        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();
        }