如何将CF_DIBV5从剪贴板(Format17)转换为透明位图?

Ram*_*eza 2 .net c# clipboard gdi+

CF_DIBV5当几个应用程序将透明图像放入剪贴板时,GDI +不支持格式化BUT,它们使用CF_DIBV5(格式17)格式来维护Alpha通道.

.NET Framework无法处理CF_DIBV5格式,因此.NET应用程序无法在剪贴板中放入或检索alpha映像.

是否有任何c#代码支持CF_DIBV5从剪贴板转换位图?

Ram*_*eza 5

抱歉!我在回答我自己的问题:

[StructLayout(LayoutKind.Sequential)]
public struct BITMAPV5HEADER
{
  public uint bV5Size;
  public int bV5Width;
  public int bV5Height;
  public UInt16 bV5Planes;
  public UInt16 bV5BitCount;
  public uint bV5Compression;
  public uint bV5SizeImage;
  public int bV5XPelsPerMeter;
  public int bV5YPelsPerMeter;
  public UInt16 bV5ClrUsed;
  public UInt16 bV5ClrImportant;
  public UInt16 bV5RedMask;
  public UInt16 bV5GreenMask;
  public UInt16 bV5BlueMask;
  public UInt16 bV5AlphaMask;
  public UInt16 bV5CSType;
  public IntPtr bV5Endpoints;
  public UInt16 bV5GammaRed;
  public UInt16 bV5GammaGreen;
  public UInt16 bV5GammaBlue;
  public UInt16 bV5Intent;
  public UInt16 bV5ProfileData;
  public UInt16 bV5ProfileSize;
  public UInt16 bV5Reserved;
}        
public static Bitmap CF_DIBV5ToBitmap(byte[] data)
{
    GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
    var bmi = (BITMAPV5HEADER)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(BITMAPV5HEADER));
    Bitmap bitmap = new Bitmap((int)bmi.bV5Width, (int)bmi.bV5Height, -
                               (int)(bmi.bV5SizeImage / bmi.bV5Height), PixelFormat.Format32bppArgb,
                               new IntPtr(handle.AddrOfPinnedObject().ToInt32()
                               + bmi.bV5Size + (bmi.bV5Height - 1) 
                               * (int)(bmi.bV5SizeImage / bmi.bV5Height)));
    handle.Free();
    return bitmap;
}   
Run Code Online (Sandbox Code Playgroud)

  • OnPaste()你可以使用:[...] if(data.GetFormats().Contains("Format17")){byte [] datab; using(MemoryStream ms =(System.IO.MemoryStream)data.GetData("Format17")){datab = ms.ToArray(); } img = Helper.CF_DIBV5ToBitmap(datab); } (2认同)