代码之家  ›  专栏  ›  技术社区  ›  Kris Erickson

从C语言中的位图对象中获取Bitmapv5Header的任何方法#

  •  0
  • Kris Erickson  · 技术社区  · 17 年前

    是否有任何方法可以从C中的位图对象中获取Bitmapv5Header?或者只是得到他们的价值观?我需要从位图中获取一些颜色空间信息,但在C中找不到这样做的方法。

    2 回复  |  直到 17 年前
        1
  •  1
  •   Samuel    17 年前

    这似乎不是一个简单的方法,但一个黑客(可能是非常笨拙)的方法是读取原始数据并将其转换为 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);
    }
    
        2
  •  0
  •   Daniel A. White    17 年前

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

    推荐文章