tags:

views:

128

answers:

2

Is there any way to get a BITMAPV5HEADER out of a Bitmap object in C#? Or just get the values that are in their? I need to get some ColorSpace information out of a bitmap and can't see a way to do this in C#.

A: 

I doubt it. The BITMAPV5HEADER is for GDI objects not GDI+ which the Bitmap is made using. I would reopen the file if possible using the standard GDI calls.

Daniel A. White
If it's not coming from a file?
Kris Erickson
+1  A: 

Doesn't seem to be an easy way to do it, but a hackish (and probably very buggy) way would be to read in the raw data and convert it to a BITMAPV5HEADER structure.

Structure
[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;
}
Helper Method
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;
}
Usage
using (FileStream stream = File.OpenRead("..."))
{
  BITMAPV5HEADER header = RawStructureRead<BITMAPV5HEADER>(stream);
}
Samuel