首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

,怎么把wbmp转成bmp格式

2012-08-21 
求助,如何把wbmp转成bmp格式?C# codeusing Systemusing System.IOusing System.Drawing.Imagingusing S

求助,如何把wbmp转成bmp格式?

C# code
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的倍数的话出来的结果是向右倾斜+平移的,请问该如何修改呢?

[解决办法]
C# code
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                    }                } 

热点排行