public byte[] UnwrapStructure(uint format) { //TODO: it is very sad that we invoke System.Drawing here to get the job done. probably not very optimal. var bitmapVersionFivePointer = ClipboardApi.GetClipboardData(ClipboardApi.CF_DIBV5); var bitmapVersionFiveHeader = (BITMAPV5HEADER)Marshal.PtrToStructure(bitmapVersionFivePointer, typeof(BITMAPV5HEADER)); if (bitmapVersionFiveHeader.bV5Compression == BI_RGB) { var bitmapVersionOneBytes = ClipboardApi.GetClipboardDataBytes(ClipboardApi.CF_DIB); var bitmapVersionOneHeader = GeneralApi.ByteArrayToStructure <BITMAPINFOHEADER>(bitmapVersionOneBytes); return(HandleBitmapVersionOne(bitmapVersionOneBytes, bitmapVersionOneHeader)); } else { return(HandleBitmapVersionFive(bitmapVersionFivePointer, bitmapVersionFiveHeader)); } }
/// <summary> /// Convert to a DataClip collection all data present in the clipboard /// </summary> /// <returns></returns> public static ReadOnlyCollection <ClipboardData> GetClipboard() { //Init a list of ClipData, which will contain each Clipboard Data List <ClipboardData> clipData = new List <ClipboardData>(); //Open Clipboard to allow us to read from it if (!ClipboardApi.OpenClipboard(IntPtr.Zero)) { return(new ReadOnlyCollection <ClipboardData>(clipData)); } //Loop for each clipboard data type uint format = 0; while ((format = ClipboardApi.EnumClipboardFormats(format)) != 0) { //Check if clipboard data type is recognized, and get its name string formatName = "0"; if (format > 14) { StringBuilder res = new StringBuilder(); if (ClipboardApi.GetClipboardFormatName(format, res, 100) > 0) { formatName = res.ToString(); } } //Get the pointer for the current Clipboard Data IntPtr pos = ClipboardApi.GetClipboardData(format); //Goto next if it's unreachable if (pos == IntPtr.Zero) { continue; } //Get the clipboard buffer data properties UIntPtr lenght = MemoryApi.GlobalSize(pos); IntPtr gLock = MemoryApi.GlobalLock(pos); byte[] buffer; if ((int)lenght > 0) { //Init a buffer which will contain the clipboard data buffer = new byte[(int)lenght]; int l = Convert.ToInt32(lenght.ToString()); //Copy data from clipboard to our byte[] buffer Marshal.Copy(gLock, buffer, 0, l); } else { buffer = new byte[0]; } //Create a ClipData object that represtens current clipboard data var cd = new ClipboardData(format, formatName, buffer); cd.FormatName = formatName; //Add current Clipboard Data to the list clipData.Add(cd); } //Close the clipboard and realese unused resources ClipboardApi.CloseClipboard(); //Returns the list of Clipboard Datas as a ReadOnlyCollection of ClipData return(new ReadOnlyCollection <ClipboardData>(clipData)); }