/// <summary>
        /// Set the content for the specified format.
        /// You will need to "lock" (OpenClipboard) the clipboard before calling this.
        /// </summary>
        /// <param name="format">the format to set the content for</param>
        /// <param name="stream">MemoryStream with the content</param>
        public static void SetAsStream(string format, MemoryStream stream)
        {
            uint formatId;

            if (!Format2Id.TryGetValue(format, out formatId))
            {
                formatId = RegisterFormat(format);
            }
            var length  = stream.Length;
            var hGlobal = Kernel32Api.GlobalAlloc(GlobalMemorySettings.ZeroInit | GlobalMemorySettings.Movable, new UIntPtr((ulong)length));

            if (hGlobal == IntPtr.Zero)
            {
                throw new Win32Exception();
            }
            var memoryPtr = Kernel32Api.GlobalLock(hGlobal);

            try
            {
                if (memoryPtr == IntPtr.Zero)
                {
                    throw new Win32Exception();
                }
                // Fill the global memory
                Marshal.Copy(stream.GetBuffer(), 0, memoryPtr, (int)length);
            }
            finally
            {
                Kernel32Api.GlobalUnlock(hGlobal);
            }
            // Place the content on the clipboard
            NativeMethods.SetClipboardData(formatId, hGlobal);
        }
        /// <summary>
        /// Retrieve the content for the specified format.
        /// You will need to "lock" (OpenClipboard) the clipboard before calling this.
        /// </summary>
        /// <param name="format">the format to retrieve the content for</param>
        /// <returns>MemoryStream</returns>
        public static MemoryStream GetAsStream(string format)
        {
            uint formatId;

            if (!Format2Id.TryGetValue(format, out formatId))
            {
                formatId = RegisterFormat(format);
            }
            var hGlobal   = NativeMethods.GetClipboardData(formatId);
            var memoryPtr = Kernel32Api.GlobalLock(hGlobal);

            try
            {
                if (memoryPtr == IntPtr.Zero)
                {
                    throw new Win32Exception();
                }
                var size   = Kernel32Api.GlobalSize(hGlobal);
                var stream = new MemoryStream(size);
                stream.SetLength(size);
                // Fill the memory stream
                Marshal.Copy(memoryPtr, stream.GetBuffer(), 0, size);
                return(stream);
            }
            finally
            {
                Kernel32Api.GlobalUnlock(hGlobal);
            }
        }
Exemple #3
0
 /// <summary>
 /// Cleanup this native info by unlocking the global handle
 /// </summary>
 public void Dispose()
 {
     Kernel32Api.GlobalUnlock(GlobalHandle);
     if (NeedsWrite)
     {
         // Place the content on the clipboard
         NativeMethods.SetClipboardData(FormatId, GlobalHandle);
     }
 }