Friday, April 3, 2015

C# BMP 파일 읽기

public class BmpLoader
{
    public struct BMPHeader
    {
        public short type;
        public int size;
        public short reserved1;
        public short reserved2;
        public int offset;
    }
    public struct BMPInfoHeader
    {
        public int size;
        public int width;
        public int height;
        public short planes;
        public short bitsPerPixel;
        public uint compression;
        public uint imageSize;
        public int xPelsPerMeter;
        public int yPelsPerMeter;
        public int clrUsed;
        public int clrImportant;
    }

    private BMPHeader header;
    private BMPInfoHeader infoHeader;
    public byte[] Load(string filename, out int width, out int height)
    {
        BinaryReader reader = new BinaryReader(File.OpenRead(filename));
        header.type = reader.ReadInt16();
        header.size = reader.ReadInt32();
        header.reserved1 = reader.ReadInt16();
        header.reserved2 = reader.ReadInt16();
        header.offset = reader.ReadInt32();
        infoHeader.size = reader.ReadInt32();
        infoHeader.width = reader.ReadInt32();
        infoHeader.height = reader.ReadInt32();
        infoHeader.planes = reader.ReadInt16();
        infoHeader.bitsPerPixel = reader.ReadInt16();
        infoHeader.compression = reader.ReadUInt32();
        infoHeader.compression = reader.ReadUInt32();
        infoHeader.xPelsPerMeter = reader.ReadInt32();
        infoHeader.yPelsPerMeter = reader.ReadInt32();
        infoHeader.clrUsed = reader.ReadInt32();
        infoHeader.clrImportant = reader.ReadInt32();
        int size = infoHeader.width * infoHeader.height * (infoHeader.bitsPerPixel / 8);
        byte[] pixels = reader.ReadBytes(size);
        width = infoHeader.width;
        height = infoHeader.height;
        return pixels;
    }
}

No comments:

Post a Comment