Beispiel #1
0
        public object GetData(TransferDataType type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            while (!IsTypeAvailable(type))
            {
                Thread.Sleep(1);
            }

            if (type == TransferDataType.Image)
            {
                return(WindowsClipboard.GetImage());
            }
            if (type == TransferDataType.Rtf)
            {
                return(WindowsClipboard.GetText(TextDataFormat.Rtf));
            }
            if (type == TransferDataType.Text)
            {
                if (WindowsClipboard.ContainsText(TextDataFormat.UnicodeText))
                {
                    return(WindowsClipboard.GetText());
                }

                return(WindowsClipboard.GetText(TextDataFormat.Text));
            }

            throw new NotImplementedException();
        }
        /// <summary>
        /// Handle pasting and handle images
        /// </summary>
        public void PasteOperation()
        {
            if (Clipboard.ContainsImage())
            {
                string imagePath = null;

                var bmpSource = Clipboard.GetImage();
                using (var bitMap = WindowUtilities.BitmapSourceToBitmap(bmpSource))
                {
                    imagePath = AddinManager.Current.RaiseOnSaveImage(bitMap);
                }
                if (!string.IsNullOrEmpty(imagePath))
                {
                    SetSelection($"![]({imagePath})");
                    PreviewMarkdownCallback(); // force a preview refresh
                    return;
                }

                string initialFolder = null;
                if (!string.IsNullOrEmpty(MarkdownDocument.Filename) && MarkdownDocument.Filename != "untitled")
                {
                    initialFolder = Path.GetDirectoryName(MarkdownDocument.Filename);
                }

                var sd = new SaveFileDialog
                {
                    Filter           = "Image files (*.png;*.jpg;*.gif;)|*.png;*.jpg;*.jpeg;*.gif|All Files (*.*)|*.*",
                    FilterIndex      = 1,
                    Title            = "Save Image from Clipboard as",
                    InitialDirectory = initialFolder,
                    CheckFileExists  = false,
                    OverwritePrompt  = true,
                    CheckPathExists  = true,
                    RestoreDirectory = true
                };
                var result = sd.ShowDialog();
                if (result != null && result.Value)
                {
                    imagePath = sd.FileName;
                    try
                    {
                        var ext = Path.GetExtension(imagePath)?.ToLower();
                        using (var fileStream = new FileStream(imagePath, FileMode.Create))
                        {
                            BitmapEncoder encoder = null;
                            if (ext == ".png")
                            {
                                encoder = new PngBitmapEncoder();
                            }
                            else if (ext == ".jpg" || ext == ".jpeg")
                            {
                                encoder = new JpegBitmapEncoder();
                            }
                            else if (ext == ".gif")
                            {
                                encoder = new GifBitmapEncoder();
                            }

                            encoder.Frames.Add(BitmapFrame.Create(bmpSource));
                            encoder.Save(fileStream);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Couldn't copy file to new location: \r\n" + ex.Message, mmApp.ApplicationName);
                        return;
                    }

                    string relPath = Path.GetDirectoryName(sd.FileName);
                    if (initialFolder != null)
                    {
                        try
                        {
                            relPath = FileUtils.GetRelativePath(sd.FileName, initialFolder);
                        }
                        catch (Exception ex)
                        {
                            mmApp.Log($"Failed to get relative path.\r\nFile: {sd.FileName}, Path: {imagePath}", ex);
                        }
                        if (!relPath.StartsWith("..\\"))
                        {
                            imagePath = relPath;
                        }
                    }

                    if (imagePath.Contains(":\\"))
                    {
                        imagePath = "file:///" + imagePath;
                    }
                    SetSelection($"![]({imagePath})");
                    PreviewMarkdownCallback(); // force a preview refresh
                }
            }
            else if (Clipboard.ContainsText())
            {
                // just paste as is at cursor or selection
                SetSelection(Clipboard.GetText());
            }
        }
        public DataPackageView GetContent()
        {
            var dataPackage = new DataPackage();

            if (Clipboard.ContainsImage())
            {
                dataPackage.SetDataProvider(StandardDataFormats.Bitmap, async ct =>
                {
                    var bitmap       = Clipboard.GetImage();
                    var bitmapStream = new MemoryStream();

                    var bitmapEncoder = new BmpBitmapEncoder();
                    bitmapEncoder.Frames.Add(BitmapFrame.Create(bitmap));
                    bitmapEncoder.Save(bitmapStream);

                    // Letting a MemoryStream run around does not cause problems.
                    // The GC will take care of it, just like a byte[].
                    return(RandomAccessStreamReference.CreateFromStream(bitmapStream.AsRandomAccessStream()));
                });
            }
            if (Clipboard.ContainsText())
            {
                // Copying significant amounts of text still makes Clipboard.GetText() slow, so
                // we'll still use the SetDataProvider
                dataPackage.SetDataProvider(StandardDataFormats.Text, async ct =>
                {
                    return(Clipboard.GetText());
                });
            }
            if (Clipboard.ContainsData(DataFormats.Html))
            {
                dataPackage.SetDataProvider(StandardDataFormats.Html, async ct =>
                {
                    return(Clipboard.GetData(DataFormats.Html));
                });
            }
            if (Clipboard.ContainsData(DataFormats.Rtf))
            {
                dataPackage.SetDataProvider(StandardDataFormats.Rtf, async ct =>
                {
                    return(Clipboard.GetData(DataFormats.Rtf));
                });
            }
            if (Clipboard.ContainsFileDropList())
            {
                dataPackage.SetDataProvider(StandardDataFormats.StorageItems, async ct =>
                {
                    var list            = Clipboard.GetFileDropList();
                    var storageItemList = new List <IStorageItem>(list.Count);
                    foreach (var path in list)
                    {
                        var attr = File.GetAttributes(path);
                        if (attr.HasFlag(global::System.IO.FileAttributes.Directory))
                        {
                            storageItemList.Add(await StorageFolder.GetFolderFromPathAsync(path));
                        }
                        else
                        {
                            storageItemList.Add(await StorageFile.GetFileFromPathAsync(path));
                        }
                    }
                    return(storageItemList);
                });
            }

            return(dataPackage.GetView());
        }