예제 #1
0
        public MainWindow()
        {
            InitializeComponent();

            System.Windows.IDataObject data = System.Windows.Clipboard.GetDataObject();

            string[] formats = data.GetFormats();

#if !DEBUG
            // Makes it a royal pain to debug...
            this.Topmost       = true;
            this.ShowInTaskbar = false;
            //this.CaptureButton.Visibility = Visibility.Collapsed;
#endif

            new Dragger(Toolbar);

            ToleranceSlider.Value = ScreenCoordinates.ColorTolerance;

            ToleranceSlider.ValueChanged += delegate
            {
                ScreenCoordinates.ColorTolerance = ToleranceSlider.Value;
            };

            DataContext = this;

            IntRect screenBounds = ScreenShot.FullScreenBounds;
            Top    = screenBounds.Top;
            Left   = screenBounds.Left;
            Width  = screenBounds.Width;
            Height = screenBounds.Height;
        }
예제 #2
0
        protected override void OnDrop(DragEventArgs e)
        {
            Win32Point wp;

            e.Effects = DragDropEffects.Copy;
            Point p = e.GetPosition(this);

            wp.x = (int)p.X;
            wp.y = (int)p.Y;
            IDropTargetHelper dropHelper = (IDropTargetHelper) new DragDropHelper();

            dropHelper.Drop((ComIDataObject)e.Data, ref wp, (int)e.Effects);
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] files = e.Data.GetData(DataFormats.FileDrop) as string[];
                this.OpenImage(files[0]);
            }

            System.Windows.IDataObject data = e.Data;
            string[] formats = data.GetFormats();
            if (formats.Contains("text/html"))
            {
                var    obj  = data.GetData("text/html");
                string html = string.Empty;
                if (obj is string)
                {
                    html = (string)obj;
                }
                else if (obj is MemoryStream)
                {
                    MemoryStream ms     = (MemoryStream)obj;
                    byte[]       buffer = new byte[ms.Length];
                    ms.Read(buffer, 0, (int)ms.Length);
                    if (buffer[1] == (byte)0)  // Detecting unicode
                    {
                        html = System.Text.Encoding.Unicode.GetString(buffer);
                    }
                    else
                    {
                        html = System.Text.Encoding.ASCII.GetString(buffer);
                    }
                }
                // Using a regex to parse HTML, but JUST FOR THIS EXAMPLE :-)
                var match = new Regex(@"<img[^>]+src=""([^""]*)""").Match(html);
                if (match.Success)
                {
                    Uri uri = new Uri(match.Groups[1].Value);
                    SetImageFromUri(uri);
                }
            }
        }
        /// <summary>
        /// Initialize a new instance of the <see cref="FormatEnumerator"/> class.
        /// </summary>
        /// <param name="dataObject">The <see cref="System.Windows.IDataObject"/></param>
        internal FormatEnumerator(System.Windows.IDataObject dataObject)
        {
            var formats = dataObject.GetFormats();

            _formats = new FORMATETC[formats == null ? 0 : formats.Length];

            if (formats != null)
            {
                for (var i = 0; i < formats.Length; i++)
                {
                    var format = formats[i];
                    var temp   = new FORMATETC();
                    temp.cfFormat = (short)DataFormats.GetDataFormat(format).Id;
                    temp.dwAspect = DVASPECT.DVASPECT_CONTENT;
                    temp.ptd      = IntPtr.Zero;
                    temp.lindex   = -1;
                    temp.tymed    = TYMED.TYMED_HGLOBAL;

                    _formats[i] = temp;
                }
            }
        }
예제 #4
0
        private void Tag_Image_Drop(object sender, System.Windows.DragEventArgs e)
        {
            //should be a URL or a file URI
            //http://stackoverflow.com/questions/8442085/receiving-an-image-dragged-from-web-page-to-wpf-window

            System.Windows.IDataObject data = e.Data;
            string[] formats = data.GetFormats();

            object obj   = null;
            bool   found = false;

            if (formats.Contains("text/html"))
            {
                obj   = data.GetData("text/html");
                found = true;
            }
            else if (formats.Contains("HTML Format"))
            {
                obj   = data.GetData("HTML Format");
                found = true;
            }
            if (found)
            {
                string html = string.Empty;
                if (obj is string)
                {
                    html = (string)obj;
                    //remove any whitespace, linebreaks
                    html = html.Replace(" ", "").Replace("\r\n", "");
                }
                else if (obj is MemoryStream)
                {
                    MemoryStream ms     = (MemoryStream)obj;
                    byte[]       buffer = new byte[ms.Length];
                    ms.Read(buffer, 0, (int)ms.Length);
                    if (buffer[1] == (byte)0)  // Detecting unicode
                    {
                        html = System.Text.Encoding.Unicode.GetString(buffer);
                    }
                    else
                    {
                        html = System.Text.Encoding.ASCII.GetString(buffer);
                    }
                }
                // Using a basic regex to parse HTML
                var match = new Regex(@"<img[^>]*src=""([^""]*)""", RegexOptions.IgnoreCase).Match(html);
                if (match.Success)
                {
                    Uri uri = new Uri(match.Groups[1].Value);
                    SetImageFromUri(uri);
                }
                else
                {
                    // Try look for a URL to an image, encoded (thanks google image search....)
                    match = new Regex(@"(url|src)=[""]?(.*?)(&|"")").Match(html);
                    //url=http%3A%2F%2Fi.imgur.com%2FK1lxb2L.jpg&amp
                    if (match.Success)
                    {
                        bool successImg = false;
                        int  i          = 0;
                        while (i < match.Groups.Count && successImg == false)
                        {
                            i++;
                            try
                            {
                                Uri uri = new Uri(Uri.UnescapeDataString(match.Groups[i].Value));
                                successImg = SetImageFromUri(uri);
                            }
                            catch (Exception)
                            {
                                //probably nothing to worry about...
                            }
                        }
                    }

                    //Samples:
                    //< IMG width = "432" height = "432" id = "irc_mi" style = "margin-top: 0px;" onload = "typeof google==='object'&amp;&amp;google.aft&amp;&amp;google.aft(this)" alt = "Image result for Aperio  Mindfield   Seasons Changing" src = "https://i1.sndcdn.com/artworks-000298160676-shcuz7-t500x500.jpg" ></ A >< !--EndFragment-- ></ DIV ></ BODY ></ HTML >
                    //<IMGclass="n3VNCb"style="margin:0px;width:585px;height:585px;"alt="Identities2(2020,File)|Discogs"src="https://img.discogs.com/wzI0NTRfX35BLMsrm-c1gFSY-qU=/fit-in/600x600/filters:strip_icc():format(jpeg):mode_rgb():quality(90)/discogs-images/R-15423379-1591302400-6122.jpeg.jpg
                }
            }
        }
예제 #5
0
        public Stream GetStreamFromIDataObject(IDataObject dataObject)
        {
            var formats = dataObject.GetFormats();

            if (!formats.Contains("FileGroupDescriptorW"))
            {
                if (!formats.Contains("FileDrop"))
                {
                    return(null);
                }

                var paths = (string[])dataObject.GetData("FileDrop");

                foreach (var path in paths)
                {
                    var attr = File.GetAttributes(path);

                    if (attr.HasFlag(FileAttributes.Directory))
                    {
                        // To-Do
                        continue;
                    }

                    return(File.OpenRead(path));
                }

                return(null);
            }

            var fileGroupDescriptorStream = (MemoryStream)dataObject.GetData("FileGroupDescriptorW", true);
            var fileGroupDescriptorBytes  = new byte[fileGroupDescriptorStream.Length];

            fileGroupDescriptorStream.Read(fileGroupDescriptorBytes, 0, fileGroupDescriptorBytes.Length);
            fileGroupDescriptorStream.Close();

            var fileGroupDescriptorWPointer = Marshal.AllocHGlobal(fileGroupDescriptorBytes.Length);

            Marshal.Copy(fileGroupDescriptorBytes, 0, fileGroupDescriptorWPointer, fileGroupDescriptorBytes.Length);

            var fileGroupDescriptorObject = Marshal.PtrToStructure(fileGroupDescriptorWPointer, typeof(FileGroupDescriptor));
            var fileGroupDescriptor       = (FileGroupDescriptor)fileGroupDescriptorObject;
            var fileNames = new string[fileGroupDescriptor.cItems];

            var fileDescriptorPointer = (IntPtr)((int)fileGroupDescriptorWPointer + Marshal.SizeOf(fileGroupDescriptor.cItems));

            for (int fileDescriptorIndex = 0; fileDescriptorIndex < fileGroupDescriptor.cItems; fileDescriptorIndex++)
            {
                var fileDescriptor = (FileDescriptor)Marshal.PtrToStructure(fileDescriptorPointer, typeof(FileDescriptor));
                fileNames[fileDescriptorIndex] = fileDescriptor.cFileName;

                fileDescriptorPointer = (IntPtr)((int)fileDescriptorPointer + Marshal.SizeOf(fileDescriptor));
            }

            Marshal.FreeHGlobal(fileGroupDescriptorWPointer);

            var comDataObject = (System.Runtime.InteropServices.ComTypes.IDataObject)dataObject;
            var dataFormat    = DataFormats.GetDataFormat("FileContents");

            for (int i = 0; i < fileGroupDescriptor.cItems; i++)
            {
                var format = new FORMATETC();
                format.cfFormat = (short)dataFormat.Id;
                format.dwAspect = DVASPECT.DVASPECT_CONTENT;
                format.lindex   = i;
                format.tymed    = TYMED.TYMED_ISTREAM | TYMED.TYMED_HGLOBAL | TYMED.TYMED_ISTORAGE | TYMED.TYMED_FILE;

                var medium = new STGMEDIUM();

                comDataObject.GetData(ref format, out medium);

                if (medium.tymed == TYMED.TYMED_HGLOBAL)
                {
                    var handle  = medium.unionmember;
                    var pointer = GlobalLock(handle);
                    var size    = GlobalSize(handle);
                    var bytes   = new byte[size];
                    Marshal.Copy(pointer, bytes, 0, size);
                    var index = 0;
                    var serializedObjectID = new Guid("FD9EA796-3B13-4370-A679-56106BB288FB").ToByteArray();
                    var isSerializedObject = false;

                    if (size > serializedObjectID.Length)
                    {
                        isSerializedObject = true;
                        for (int j = 0; j < serializedObjectID.Length; j++)
                        {
                            if (serializedObjectID[j] != bytes[j])
                            {
                                isSerializedObject = false;
                                break;
                            }
                        }

                        if (isSerializedObject)
                        {
                            index = serializedObjectID.Length;
                        }
                    }

                    GlobalUnlock(handle);

                    var memoryStream = new MemoryStream(bytes, index, bytes.Length - index);

                    if (isSerializedObject)
                    {
                        var formatter = new BinaryFormatter();
                        formatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
                        var deserializedObject = formatter.Deserialize(memoryStream);
                    }

                    return(memoryStream);
                }

                if (medium.tymed == TYMED.TYMED_ISTREAM)
                {
                    var iStream = (IStream)Marshal.GetObjectForIUnknown(medium.unionmember);
                    Marshal.Release(medium.unionmember);

                    var iStreamStat = new STATSTG();
                    iStream.Stat(out iStreamStat, 0);
                    var iStreamSize = (int)iStreamStat.cbSize;

                    var iStreamContent = new byte[iStreamSize];
                    iStream.Read(iStreamContent, iStreamContent.Length, IntPtr.Zero);

                    Marshal.ReleaseComObject(iStream);
                    return(new MemoryStream(iStreamContent));
                }
            }

            return(null);
        }
예제 #6
0
        private void Tag_Image_Drop(object sender, System.Windows.DragEventArgs e)
        {
            //should be a URL or a file URI
            //http://stackoverflow.com/questions/8442085/receiving-an-image-dragged-from-web-page-to-wpf-window

            System.Windows.IDataObject data = e.Data;
            string[] formats = data.GetFormats();

            object obj   = null;
            bool   found = false;

            if (formats.Contains("text/html"))
            {
                obj   = data.GetData("text/html");
                found = true;
            }
            else if (formats.Contains("HTML Format"))
            {
                obj   = data.GetData("HTML Format");
                found = true;
            }
            if (found)
            {
                string html = string.Empty;
                if (obj is string)
                {
                    html = (string)obj;
                }
                else if (obj is MemoryStream)
                {
                    MemoryStream ms     = (MemoryStream)obj;
                    byte[]       buffer = new byte[ms.Length];
                    ms.Read(buffer, 0, (int)ms.Length);
                    if (buffer[1] == (byte)0)  // Detecting unicode
                    {
                        html = System.Text.Encoding.Unicode.GetString(buffer);
                    }
                    else
                    {
                        html = System.Text.Encoding.ASCII.GetString(buffer);
                    }
                }
                // Using a regex to parse HTML, but JUST FOR THIS EXAMPLE :-)
                var match = new Regex(@"<img[^/]src=""([^""]*)""").Match(html);
                if (match.Success)
                {
                    Uri uri = new Uri(match.Groups[1].Value);
                    SetImageFromUri(uri);
                }
                else
                {
                    // Try look for a URL to an image, encoded (thanks google image search....)
                    match = new Regex(@"url=(.*?)&").Match(html);
                    //url=http%3A%2F%2Fi.imgur.com%2FK1lxb2L.jpg&amp
                    if (match.Success)
                    {
                        Uri uri = new Uri(Uri.UnescapeDataString(match.Groups[1].Value));
                        SetImageFromUri(uri);
                    }
                }
            }
        }