/// <summary>
        /// A special format 17 bitmap reader
        /// </summary>
        /// <param name="clipboardAccessToken">IClipboardAccessToken</param>
        /// <returns>Bitmap or null</returns>
        public static Bitmap GetAsFormat17(this IClipboardAccessToken clipboardAccessToken)
        {
            var formats = clipboardAccessToken.AvailableFormats().ToList();

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

            var format17Bytes = clipboardAccessToken.GetAsBytes("Format17");
            var infoHeader    = BinaryStructHelper.FromByteArray <BitmapInfoHeader>(format17Bytes);

            if (!infoHeader.IsDibV5)
            {
                return(null);
            }

            // Using special DIBV5 / Format17 format reader
            // CF_DIBV5
            var gcHandle = IntPtr.Zero;

            try
            {
                var handle = GCHandle.Alloc(format17Bytes, GCHandleType.Pinned);
                gcHandle = GCHandle.ToIntPtr(handle);
                return
                    (new Bitmap(infoHeader.Width, infoHeader.Height,
                                -(int)(infoHeader.SizeImage / infoHeader.Height),
                                infoHeader.BitCount == 32 ? PixelFormat.Format32bppArgb : PixelFormat.Format24bppRgb,
                                new IntPtr(handle.AddrOfPinnedObject().ToInt32() + infoHeader.OffsetToPixels + (infoHeader.Height - 1) * (int)(infoHeader.SizeImage / infoHeader.Height))
                                ));
            }
            catch (Exception ex)
            {
                Log.Error().WriteLine(ex, "Problem retrieving Format17 from clipboard.");
            }
            finally
            {
                if (gcHandle == IntPtr.Zero)
                {
                    GCHandle.FromIntPtr(gcHandle).Free();
                }
            }

            return(null);
        }
        /// <summary>
        /// Get a DIB from the Clipboard
        /// </summary>
        /// <param name="clipboardAccessToken"></param>
        /// <returns>Bitmap or null</returns>
        public static Bitmap GetAsDeviceIndependendBitmap(this IClipboardAccessToken clipboardAccessToken)
        {
            var formats = clipboardAccessToken.AvailableFormats().ToList();

            if (!formats.Contains(StandardClipboardFormats.Bitmap.AsString()))
            {
                return(null);
            }

            var format17Bytes = clipboardAccessToken.GetAsBytes(StandardClipboardFormats.Bitmap.AsString());
            var infoHeader    = BinaryStructHelper.FromByteArray <BitmapInfoHeader>(format17Bytes);

            if (infoHeader.IsDibV5)
            {
                Log.Warn().WriteLine("Getting DIBV5 (format 17) when requesting DIB");
                return(null);
            }

            // Bitmap version older than 5
            var fileHeaderSize  = Marshal.SizeOf(typeof(BitmapFileHeader));
            var fileHeader      = BitmapFileHeader.Create(infoHeader);
            var fileHeaderBytes = BinaryStructHelper.ToByteArray(fileHeader);

            using (var bitmapStream = new MemoryStream())
            {
                bitmapStream.Write(fileHeaderBytes, 0, fileHeaderSize);
                bitmapStream.Write(format17Bytes, 0, format17Bytes.Length);
                bitmapStream.Seek(0, SeekOrigin.Begin);
                var image = BitmapHelper.FromStream(bitmapStream);
                if (image != null)
                {
                    return(image);
                }
            }
            return(null);
        }
 /// <summary>
 /// Retrieve the content for the specified format.
 /// You will need to "lock" (OpenClipboard) the clipboard before calling this.
 /// </summary>
 /// <param name="clipboardAccessToken">IClipboardLock</param>
 /// <param name="format">string with the format to retrieve the content for</param>
 /// <returns>byte array</returns>
 public static byte[] GetAsBytes(this IClipboardAccessToken clipboardAccessToken, string format)
 {
     return(clipboardAccessToken.GetAsBytes(ClipboardFormatExtensions.MapFormatToId(format)));
 }
 /// <summary>
 /// Retrieve the content for the specified format.
 /// You will need to "lock" (OpenClipboard) the clipboard before calling this.
 /// </summary>
 /// <param name="clipboardAccessToken">IClipboardLock</param>
 /// <param name="format">StandardClipboardFormats with the format to retrieve the content for</param>
 /// <returns>byte array</returns>
 public static byte[] GetAsBytes(this IClipboardAccessToken clipboardAccessToken, StandardClipboardFormats format)
 {
     return(clipboardAccessToken.GetAsBytes((uint)format));
 }
Exemple #5
0
        /// <summary>
        /// Get a string from the clipboard, this assumes you already locked the clipboard.
        /// This by default takes the CF_UNICODETEXT format, as Windows automatically converts
        /// </summary>
        /// <param name="clipboardAccessToken">IClipboardLock</param>
        /// <param name="formatId">uint with the clipboard format</param>
        /// <returns>string</returns>
        public static string GetAsUnicodeString(this IClipboardAccessToken clipboardAccessToken, uint formatId = (uint)StandardClipboardFormats.UnicodeText)
        {
            var bytes = clipboardAccessToken.GetAsBytes(formatId);

            return(Encoding.Unicode.GetString(bytes, 0, bytes.Length).TrimEnd('\0'));
        }