Ejemplo n.º 1
0
        public static PalasoImage GetImageFromClipboard()
        {
            // N.B.: PalasoImage does not handle .svg files
#if MONO
            if (GtkContainsImage())
            {
                return(PalasoImage.FromImage(GtkGetImage()));
            }

            if (GtkContainsText())
            {
                //REVIEW: I can find no documentation on GtkClipboard. If ContainsText means we have a file
                //	path, then it would be better to do PalasoImage.FromFileRobustly(); on the file path
                return(PalasoImage.FromImage(GtkGetImageFromText()));
            }

            return(null);
#else
            var dataObject = Clipboard.GetDataObject();
            if (dataObject == null)
            {
                return(null);
            }

            var textData = String.Empty;
            if (dataObject.GetDataPresent(DataFormats.UnicodeText))
            {
                textData = dataObject.GetData(DataFormats.UnicodeText) as String;
            }
            if (Clipboard.ContainsImage())
            {
                PalasoImage plainImage = null;
                try
                {
                    plainImage = PalasoImage.FromImage(Clipboard.GetImage());                     // this method won't copy any metadata
                    var haveFileUrl = !String.IsNullOrEmpty(textData) && RobustFile.Exists(textData);

                    // If we have an image on the clipboard, and we also have text that is a valid url to an image file,
                    // use the url to create a PalasoImage (which will pull in any metadata associated with the image too)
                    if (haveFileUrl)
                    {
                        var imageWithPathAndMaybeMetadata = PalasoImage.FromFileRobustly(textData);
                        plainImage.Dispose();                        //important: don't do this until we've successfully created the imageWithPathAndMaybeMetadata
                        return(imageWithPathAndMaybeMetadata);
                    }
                    else
                    {
                        return(plainImage);
                    }
                }
                catch (Exception e)
                {
                    Logger.WriteEvent("PortableClipboard.GetImageFromClipboard() failed with message " + e.Message);
                    return(plainImage);                    // at worst, we should return null; if FromFile() failed, we return an image
                }
            }
            // the ContainsImage() returns false when copying an PNG from MS Word
            // so here we explicitly ask for a PNG and see if we can convert it.
            if (dataObject.GetDataPresent("PNG"))
            {
                var o = dataObject.GetData("PNG") as Stream;
                try
                {
                    return(PalasoImage.FromImage(Image.FromStream(o)));
                }
                catch (Exception)
                {}
            }

            //People can do a "copy" from the WIndows Photo Viewer but what it puts on the clipboard is a path, not an image
            if (dataObject.GetDataPresent(DataFormats.FileDrop))
            {
                //This line gets all the file paths that were selected in explorer
                string[] files = dataObject.GetData(DataFormats.FileDrop) as string[];
                if (files == null)
                {
                    return(null);
                }

                foreach (var file in files.Where(f => RobustFile.Exists(f)))
                {
                    try
                    {
                        return(PalasoImage.FromFileRobustly(file));
                    }
                    catch (Exception)
                    {}
                }

                return(null);                //not an image
            }

            if (!Clipboard.ContainsText() || !RobustFile.Exists(Clipboard.GetText()))
            {
                return(null);
            }

            try
            {
                return(PalasoImage.FromImage(Image.FromStream(new FileStream(Clipboard.GetText(), FileMode.Open))));
            }
            catch (Exception)
            {}

            return(null);
#endif
        }