【问题标题】:Any way to get a BITMAPV5HEADER out of a Bitmap object in C#从 C# 中的位图对象中获取 BITMAPV5HEADER 的任何方法
【发布时间】:2010-10-17 10:52:49
【问题描述】:

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

【问题讨论】:

    标签: c# .net gdi+ gdi


    【解决方案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);
    }
    

    【讨论】:

    • 标头中的IntPtr bV5Endpoints应替换为包含3个uint的结构,请检查CIEXYZTRIPLE的定义
    【解决方案2】:

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

    【讨论】:

    • 如果不是来自文件?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-15
    • 1970-01-01
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多