有没有办法从 C# 中的位图对象中获取 BITMAPV5HEADER?或者只是得到他们的价值观?我需要从位图中获取一些 ColorSpace 信息,但在 C# 中看不到这样做的方法。
Kris Erickson
问问题
538 次
2 回答
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);
}
于 2009-04-03T19:38:07.250 回答
0
我对此表示怀疑。BITMAPV5HEADER
用于 GDI 对象,而不是使用的 GDI + Bitmap
。如果可能,我会使用标准 GDI 调用重新打开文件。
于 2009-04-03T19:37:30.620 回答