private void ColorBMPToOne(String filename,String toFilename) {
FileInfo file = new FileInfo(filename);
if (file.Exists)
{
FileStream fs = file.OpenRead();
byte[] bmphead = new byte[54];
fs.Read(bmphead, 0, 54);
if (bmphead[0].ToString().Equals("66") && bmphead[1].ToString().Equals("77"))
{
int bmpwidth = int.Parse(bmphead[18].ToString()) + int.Parse(bmphead[19].ToString()) * 256 + int.Parse(bmphead[20].ToString()) * 256 * 256 + int.Parse(bmphead[21].ToString()) * 256 * 256 * 256;
int bmpheight = int.Parse(bmphead[22].ToString()) + int.Parse(bmphead[23].ToString()) * 256 + int.Parse(bmphead[24].ToString()) * 256 * 256 + int.Parse(bmphead[25].ToString()) * 256 * 256 * 256; ;
int bit = int.Parse(bmphead[28].ToString());
if ((bit == 24) || (bit == 32))
{
}
else {
MessageBox.Show("请确定图位数为24或者32位图");
return;
}
int bblength = bmpwidth * 3;
int bbend = 0;
if ((bmpwidth*3 % 4) > 0)
{
bbend=4 - ((bmpwidth * 3) % 4);
bblength = bmpwidth * 3 + bbend;
}
int newbmpwidth = 0;
if((bmpwidth%32)>0){
newbmpwidth = ((32 - bmpwidth % 32) + bmpwidth) / 8;
}else{
newbmpwidth = bmpwidth / 8;
}
int bmpsize = newbmpwidth * bmpheight;
//图片所占的大小(算上数据头62位的)
int bmprelsize = bmpsize + 54 + 8; //54是bmp表头数据.8位是颜色表,分别为00,00,00,00,ff,ff,ff,00
byte[] bmpbit = new byte[bmprelsize + newbmpwidth];
Array.Copy(bmphead, bmpbit, 54);
#region 写表头的数据
bmpbit[10] = 0x3e;
bmpbit[28] = 0x01;
bmpbit[54] = 0x00;
bmpbit[55] = 0x00;
bmpbit[56] = 0x00;
bmpbit[57] = 0x00;
bmpbit[58] = 0xff;
bmpbit[59] = 0xff;
bmpbit[60] = 0xff;
bmpbit[61] = 0x00;
//图所占的空间大小,这个不包括图头的
String bmpsizeString = String.Format("{0:x2}", bmpsize);
String bmpresizeString = String.Format("{0:x2}", bmprelsize);
bmpsizeString = bmpsizeString.PadLeft(8, '0');
bmpresizeString = bmpresizeString.PadLeft(8, '0');
Char[] chsize = bmpsizeString.ToCharArray();
Char[] chresize = bmpresizeString.ToCharArray();
for (int i = 0; i < 8; i = i + 2)
{
bmpbit[37 - (i / 2)] = (byte)(Uri.FromHex(chsize[i]) * 16 + Uri.FromHex(chsize[i + 1]));
bmpbit[5 - (i / 2)] = (byte)(Uri.FromHex(chresize[i]) * 16 + Uri.FromHex(chresize[i + 1]));
}
#endregion
MessageBox.Show(bmpwidth.ToString() + "@@@" + bmpheight.ToString() + "@@@" + newbmpwidth.ToString() + "@@@" + bmprelsize.ToString() + "@@@" + fs.Length+"@@"+bblength.ToString());
#region
byte[] bbrec = new byte[bblength];
byte[] bb = new byte[bmpwidth*3];
int[] ch = new int[8];
int[] chend = new int[8];
int count = 0;
int count2 = 0;
int count3 = 0;
char[] cc = new char[2];
int bmpheightcount = 0;
int bmpendcount = 0;
while (fs.Read(bbrec, 0, bblength) > 0)
{
Array.Copy(bbrec, 0, bb, 0, bmpwidth * 3);
for (int k = 0; k < bb.Length-23 ; k = k + 24)
{
bmpendcount = k/24;
for (int j = 0; j < 8; j++)
{
count = k + j * 3;
if ((bb[count].ToString().Equals("255")) && (bb[count + 1].ToString().Equals("255")) && (bb[count + 2].ToString().Equals("255")))
{
ch[j] = 1;
}
else
{
ch[j] = 0;
}
}
count2 = ch[0] * 8 + ch[1] * 4 + ch[2] * 2 + ch[3];
count3 = ch[4] * 8 + ch[5] * 4 + ch[6] * 2 + ch[7];
cc[1] = Convert.ToChar(count3.ToString("x"));
cc[0] = Convert.ToChar(count2.ToString("x"));
int rewrite = k / 24 + newbmpwidth * bmpheightcount + 62;
bmpbit[rewrite] = (byte)(Uri.FromHex(cc[0]) * 16 + Uri.FromHex(cc[1]));
}
#region
int m = bb.Length% 24;
ch = new int[8];
for (int i = 0; i < ch.Length; i++)
{
ch[i] = 1;
}
if (m > 0)
{
bmpendcount = bmpendcount + 1;
for (int i = 0; i < m; i = i + 3)
{
int j = bb.Length - m + i;
if ((bb[j].ToString().Equals("255")) && (bb[j + 1].ToString().Equals("255")) && (bb[j + 2].ToString().Equals("255")))
{
ch[i / 3] = 1;
}
else
{
ch[i / 3] = 0;
}
}
count2 = ch[0] * 8 + ch[1] * 4 + ch[2] * 2 + ch[3];
count3 = ch[4] * 8 + ch[5] * 4 + ch[6] * 2 + ch[7];
cc[1] = Convert.ToChar(count3.ToString("x"));
cc[0] = Convert.ToChar(count2.ToString("x"));
bmpbit[bmpendcount + newbmpwidth * bmpheightcount + 62] = (byte)(Uri.FromHex(cc[0]) * 16 + Uri.FromHex(cc[1]));
}
#endregion
for (int i = bmpendcount+1; i < newbmpwidth;i++ ) {
bmpbit[i + newbmpwidth * bmpheightcount + 62] = (byte)Convert.ToInt16(255);
}
for (int i = bmpbit.Length; i > (bmpbit.Length - newbmpwidth);i--)
{
bmpbit[i-1] = (byte)Convert.ToInt16(255);
}
bmpheightcount = bmpheightcount + 1;
bb = new byte[bmpwidth * 3];
}
MessageBox.Show(bmpheightcount.ToString());
FileStream fwrite = new FileStream(toFilename, FileMode.Create);
fwrite.Write(bmpbit, 0, bmpbit.Length);
fwrite.Close();
fs.Close();
#endregion
}
else {
MessageBox.Show("非BMP格式的数据");
}
}
else {
MessageBox.Show("文件路径不对,请确定文件路径");
}
}
因为打印机的显示与微软的显示不一样,所以要转的时候要填充后面的补的字节,不然在打印的时候会有一条黑线