69,371
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// ReadBmp();
}
const int BFT_BITMAP=0x4d42; /* 'BMP' */
const int BI_RGB= 0;
const int BI_RLE8= 1;
const int BI_RLE4 =2;
const int BI_BITFIELDS = 3;
int ReadBmp(string FileName, ref Byte[] pImg,ref int width,ref int height)
{
ushort bfType,biBitCount;
uint biSize,dwCompression, biClrUsed, bfOffBits;
int biWidth,biHeight;
uint dwEffWidth;
int i;
FileStream filestream = new FileStream(FileName, FileMode.Open,FileAccess.Read);
BinaryReader bReader = new BinaryReader(filestream);
bReader.BaseStream.Position = 0;
bfType = bReader.ReadUInt16();
if (bfType != BFT_BITMAP)
{
Console.WriteLine("Not bmp File!");
return 0;
}
bReader.BaseStream.Position = 10;
bfOffBits =(uint) bReader.ReadInt32();
biSize = (uint)bReader.ReadInt32();
if (biSize!=40)
{
Console.WriteLine("Not common BITMAPINFOHEADER, BITMAPCOREHEADER=12!");
return 0;
}
biWidth = bReader.ReadInt32();
biHeight = bReader.ReadInt32();
bReader.BaseStream.Position += 2;
biBitCount = (ushort)bReader.ReadInt16();
dwCompression = (uint)bReader.ReadInt32();
bReader.BaseStream.Position += 12;
biClrUsed = (uint)bReader.ReadInt32();
if (dwCompression!=BI_RGB)
{
Console.WriteLine("Not supported Compression!");
bReader.Close();
filestream.Close();
return 0;
}
dwEffWidth = ((uint)(((biBitCount * biWidth) + 31) / 32) * 4);
width = biWidth;
height = biHeight;
Console.WriteLine("{1},{2}", biWidth, biHeight);
bReader.BaseStream.Position += bfOffBits;
for (i = 0; i < biHeight;++i )
{
// bReader.Read(pImg+(biHeight-1-i)*biWidth*3,0,dwEffWidth);
//这地方你自己修改下,把数据的位置放在中间的那个参数就可以了。
}
bReader.Close();
filestream.Close();
return 0;
}
}
}