日期:2014-05-18 浏览次数:20724 次
using System; using System.IO; using System.Drawing.Imaging; using System.Drawing; namespace wbmptobmp { class Program { static void Main(string[] args) { foreach (string s in args) { convertWbmp2Bmp(s); } } public static void convertWbmp2Bmp(String s) { byte[] datas = File.ReadAllBytes(s); byte tmp; int width = 0; int height = 0; int offset = 2; do { tmp = datas[offset++]; width = (width << 7) | (tmp & 0x7f); } while ((tmp & 0x80) != 0); do { tmp = datas[offset++]; height = (height << 7) | (tmp & 0x7f); } while ((tmp & 0x80) != 0); Bitmap bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed); BitmapData bd = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, bmp.PixelFormat); int stride = (width + 7) >> 3; Console.WriteLine(stride); byte[] tmpdata = new byte[height * width]; for (int i = 0; i < height; i++) { int pos = stride * i; byte mask = 0x80; for (int j = 0; j < width; j++) { if ((datas[offset + pos] & mask) == 0) tmpdata[i * width + j] = 0; else tmpdata[i * width + j] = 0xff; mask >>= 1; if (mask == 0) { mask = 0x80; pos++; } } } System.Runtime.InteropServices.Marshal.Copy(tmpdata, 0, bd.Scan0, tmpdata.Length); bmp.UnlockBits(bd); bmp.Save(s+".bmp");
for (int j = 0; j < width; j++) { if ((datas[offset + pos] & checker) == 0) { bmp.SetPixel(j, i, Color.Black); } else { bmp.SetPixel(j, i, Color.White); } checker >>= 1;//Right shift to check the next bit if (checker == 0) { checker = 0x80; pos++;//If all eight bits are checked, reset the checker and go forth } }