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");这是我的代码,但是如果源图宽度不是8的倍数的话出来的结果是向右倾斜+平移的,请问该如何修改呢?

解决方案 »

  1.   

    找到个代码,你试一试 C#将wbmp格式图片转为jpg的图片
    http://blog.csdn.net/jiajinhao/article/details/6904877代码是读到Bitmap对象中,最后保存的时候改个格式就可以了。
      

  2.   


    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
                        }
                    }
      

  3.   

    木听过 不过先转成Bitmap 对象 在保存就好弄了