How to convert Wbmp to Png?

人走茶凉 提交于 2019-12-01 05:55:41

This seems to get the job done.

using System.Drawing;
using System.IO;

namespace wbmp2png
{
    class Program
    {
        static void Main(string[] args)
        {
            foreach (string file in args)
            {
                if (!File.Exists(file))
                {
                    continue;
                }
                byte[] data = File.ReadAllBytes(file);
                int width = 0;
                int height = 0;
                int i = 2;
                for (; data[i] >> 7 == 1; i++)
                {
                    width = (width << 7) | (data[i] & 0x7F);
                }
                width = (width << 7) | (data[i++] & 0x7F);
                for (; data[i] >> 7 == 1; i++)
                {
                    height = (height << 7) | (data[i] & 0x7F);
                }
                height = (height << 7) | (data[i++] & 0x7F);
                int firstPixel = i;
                Bitmap png = new Bitmap(width, height);
                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        png.SetPixel(x, y, (((data[firstPixel + (x / 8) + (y * ((width - 1) / 8 + 1))] >> (7 - (x % 8))) & 1) == 1) ? Color.White : Color.Black);
                    }
                }
                png.Save(Path.ChangeExtension(file, "png"));
            }
        }
    }
}

Try this code, this works!

using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

class WBmpConvertor
{
   public void Convert(string inputFile, string outputFile, ImageFormat format)
    {
        byte[] datas = File.ReadAllBytes(inputFile);
        byte tmp;
        int width = 0, height = 0, 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);

        var 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;
        var 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++;
                }
            }
        }
        Marshal.Copy(tmpdata, 0, bd.Scan0, tmpdata.Length);

        bmp.UnlockBits(bd);
        bmp.Save(outputFile, format);
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!