예제 #1
0
        /// <summary>
        /// Parses the ClipboardItem as a FileDrop (string[]).
        /// </summary>
        public static string[] ParseFileDrop(ClipboardItem item)
        {
            if (!CanParse(item, AbstractDataFormat.FileDrop))
            {
                throw new ArgumentException("The ClipboardItem does not contain data in the requested format.");
            }

            switch (item.Format)
            {
            case DataFormat.HDrop:
            {
                byte[] bytes = item.GetDataBuffer();
                // The first 18 bytes are object header and size information.
                // The rest is a block of zero-terminated unicode strings.
                string   fileNamesZero = Encoding.Unicode.GetString(bytes, 18, bytes.Length - 18);
                string[] fileNames     = fileNamesZero
                                         .Split('\0')
                                         .Where(x => x.Length > 0)
                                         .ToArray();

                return(fileNames);
            }

            case DataFormat.ShellIdList:
            {
                byte[]             bytes = item.GetDataBuffer();
                NativeMethods.CIDA cida  = BinaryStructConverter.FromByteArray <NativeMethods.CIDA>(bytes);

                return(new string[] { });
            }

            default:
                throw new NotImplementedException();
            }
        }
예제 #2
0
        public static byte[] GetBytes(ClipboardItem item, AbstractDataFormat type)
        {
            switch (type)
            {
            case AbstractDataFormat.Image:
            {
                BitmapFrame      frame = ParseImage(item);
                PngBitmapEncoder png   = new PngBitmapEncoder();
                png.Frames.Add(frame);
                using (MemoryStream mem = new MemoryStream())
                {
                    png.Save(mem);
                    return(mem.ToArray());
                }
            }

            case AbstractDataFormat.Text:
            {
                string text = ParseText(item);
                return(Encoding.Unicode.GetBytes(text));
            }

            default:
                throw new NotSupportedException();
            }
        }
예제 #3
0
        /// <summary>
        /// Parses the ClipboardItem as an image (BitmapFrame).
        /// </summary>
        public static BitmapFrame ParseImage(ClipboardItem item)
        {
            if (!CanParse(item, AbstractDataFormat.Image))
            {
                throw new ArgumentException("The ClipboardItem does not contain data in the requested format.");
            }

            switch (item.Format)
            {
            case DataFormat.Dib:
                byte[]      dibBytes    = item.GetDataBuffer();
                BitmapFrame imageSource = DIBitmap.CreateBitmapFrameFromDibBytes(dibBytes);
                // Freeze the image to prevent modifications
                // and allow it to be used from different threads.
                imageSource.Freeze();
                return(imageSource);

            default:
                throw new NotImplementedException();
            }
        }
예제 #4
0
        /// <summary>
        /// Parses the ClipboardItem as text (string).
        /// </summary>
        public static string ParseText(ClipboardItem item)
        {
            if (!CanParse(item, AbstractDataFormat.Text))
            {
                throw new ArgumentException("The ClipboardItem does not contain data in the requested format.");
            }

            switch (item.Format)
            {
            case DataFormat.Text:
                return(ParseStringAZ(item.GetDataBuffer()));

            case DataFormat.UnicodeText:
                return(ParseStringWZ(item.GetDataBuffer()));

            case DataFormat.OemText:
                return(ParseStringAZ(item.GetDataBuffer()));

            default:
                throw new NotImplementedException();
            }
        }
예제 #5
0
 /// <summary>
 /// Checks if the ClipboardItem contains data
 /// that can be parsed as the requested type.
 /// </summary>
 /// <param name="item">The ClipboardItem.</param>
 /// <param name="type">The requested abstract data type.</param>
 /// <returns>True when the fragment contains data that can be parsed as the requested type.</returns>
 public static bool CanParse(ClipboardItem item, AbstractDataFormat type)
 {
     return(GetAbstractFormat(item.Format) == type);
 }