有任何方法可以从C#中的Bitmap对象中获取BITMAPV5HEADER

时间:2009-04-03 19:33:09

标签: c# .net gdi+ gdi

有没有办法从C#中的Bitmap对象中获取BITMAPV5HEADER?或者只是获取他们的价值?我需要从位图中获取一些ColorSpace信息,并且无法在C#中看到这样做的方法。

2 个答案:

答案 0 :(得分:1)

似乎不是一种简单的方法,但是一种hackish(可能非常错误)的方式是读取原始数据并将其转换为BITMAPV5HEADER结构。

结构体
[StructLayout(LayoutKind.Sequential)]
public struct BITMAPV5HEADER
{
  uint bV5Size;
  long bV5Width;
  long bV5Height;
  int bV5Planes;
  int bV5BitCount;
  uint bV5Compression;
  uint bV5SizeImage;
  long bV5XPelsPerMeter;
  long bV5YPelsPerMeter;
  uint bV5ClrUsed;
  uint bV5ClrImportant;
  uint bV5RedMask;
  uint bV5GreenMask;
  uint bV5BlueMask;
  uint bV5AlphaMask;
  uint bV5CSType;
  IntPtr bV5Endpoints;
  uint bV5GammaRed;
  uint bV5GammaGreen;
  uint bV5GammaBlue;
  uint bV5Intent;
  uint bV5ProfileData;
  uint bV5ProfileSize;
  uint bV5Reserved;
}
辅助方法
public static T RawStructureRead<T>(Stream stream) where T : struct
{
  T @struct;
  int size = Marshal.SizeOf(typeof(T));

  BinaryReader reader = new BinaryReader(stream);
  byte[] buffer = reader.ReadBytes(size);

  GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
  @struct = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
  handle.Free();

  return @struct;
}
用法
using (FileStream stream = File.OpenRead("..."))
{
  BITMAPV5HEADER header = RawStructureRead<BITMAPV5HEADER>(stream);
}

答案 1 :(得分:0)

我对此表示怀疑。 BITMAPV5HEADER用于GDI对象而不是GDI + Bitmap是使用的。如果可能的话,我会使用标准的GDI调用重新打开文件。