Exemple #1
0
        /// <summary>
        /// Places nonpersistent data on the system clipboard.
        /// </summary>
        /// <param name="data">The data to place on the clipboard.</param>
        /// <exception cref="System.ArgumentNullException">The value of data is null.</exception>
        public static void SetDataObject(IDataObject data)
        {
            if (data == null)
            {
                throw new ArgumentNullException("The value of data is null");
            }
            else
            {
                if (!OpenClipboard(IntPtr.Zero))
                {
                    throw new System.Runtime.InteropServices.ExternalException("Could not open Clipboard");
                }

                if (!EmptyClipboard())
                {
                    throw new System.Runtime.InteropServices.ExternalException("Unable to empty Clipboard");
                }

                //from here on we are only supporting unicode text, but it would be possible to support other formats
                if (data.GetDataPresent(DataFormats.UnicodeText))
                {
                    string unicodedata;
                    IntPtr hClipboard;

                    try
                    {
                        //extract unicode string from supplied data
                        unicodedata = data.GetData(DataFormats.UnicodeText).ToString();
                    }
                    catch
                    {
                        throw new FormatException("Clipboard data not in a recognised format");
                    }

                    //marshall the string
                    hClipboard = MarshalEx.StringToHGlobalUni(unicodedata);

                    //pass data to clipboard
                    if (SetClipboardData(ClipboardFormats.UnicodeText, hClipboard) == IntPtr.Zero)
                    {
                        throw new System.Runtime.InteropServices.ExternalException("Could not put data on Clipboard");
                    }
                }
                else if (data.GetDataPresent(DataFormats.Bitmap))
                {
                    System.Drawing.Bitmap bmp = (System.Drawing.Bitmap)data.GetData(DataFormats.Bitmap);

                    int bs = bmp.Width * 3 * bmp.Height;
                    //allocate unmanaged memory
                    IntPtr hClipboard = MarshalEx.AllocHGlobal(bs + 41);
                    byte[] buffer     = new byte[40];

                    MemoryStream strm = new MemoryStream(buffer);
                    BinaryWriter wr   = new BinaryWriter(strm);
                    wr.Write(40);
                    wr.Write(bmp.Width);
                    wr.Write(bmp.Height);
                    wr.Write((short)1);
                    wr.Write((short)0x18);
                    // remaining bytes will be 0
                    wr.Close();

                    MarshalEx.WriteByteArray(hClipboard, 0, buffer);
                    int pos = 40;
                    for (int r = bmp.Height - 1; r >= 0; r--)
                    {
                        for (int c = 0; c < bmp.Width; c++)
                        {
                            int color = bmp.GetPixel(c, r).ToArgb();

                            byte red   = (byte)((color & 0x00ff0000) >> 16);
                            byte green = (byte)((color & 0x0000ff00) >> 8);
                            byte blue  = (byte)(color & 0x000000ff);

                            MarshalEx.WriteByte(hClipboard, pos++, blue);
                            MarshalEx.WriteByte(hClipboard, pos++, green);
                            MarshalEx.WriteByte(hClipboard, pos++, red);
                        }
                    }


                    //pass data to clipboard
                    if (SetClipboardData(ClipboardFormats.Bitmap, hClipboard) == IntPtr.Zero)
                    {
                        throw new System.Runtime.InteropServices.ExternalException("Could not put data on Clipboard");
                    }
                }

                if (!CloseClipboard())
                {
                    throw new System.Runtime.InteropServices.ExternalException("Could not close Clipboard");
                }
            }
        }