如何将Wbmp转换为Png?

时间:2012-08-13 02:02:12

标签: c# image png bmp

在Google上花了很多时间研究这个问题之后,我无法看到在C#中将Wbmp图像转换为Png格式的示例 我从互联网上下载了一些Wbmp图像,我正在使用二进制编辑器查看它们。

有没有人有一个算法可以帮助我这样做,或者任何代码也会有所帮助。

到目前为止我所知道的事情:

  1. 第一个字节是*(单色图像为0)
  2. 第二个字节称为“固定标头”,为0
  3. 第三个字节是图像的宽度,以像素为单位*
  4. 第四个字节是图像的高度,以像素为单位*
  5. 按行排列的数据字节 - 每像素一位: 如果行长度不能被8整除,则行为0填充为 字节边界
  6. 我完全迷失了,所以我们将不胜感激任何帮助


    其他一些代码:

    using System.Drawing;
    using System.IO;
    
    class GetPixel
    {
       public static void Main(string[] args)
       {
          foreach ( string s in args )
          {
             if (File.Exists(s))
             {
                var image = new Bitmap(s);
                Color p = image.GetPixel(0, 0);
                System.Console.WriteLine("R: {0} G: {1} B: {2}", p.R, p.G, p.B);
             }
          }
       }
    }
    

    class ConfigChecker
    {
       public static void Main()
       {
          string drink = "Nothing";
          try
          {
             System.Configuration.AppSettingsReader configurationAppSettings 
                = new System.Configuration.AppSettingsReader();
             drink = ((string)(configurationAppSettings.GetValue("Drink", typeof(string))));
          }
          catch ( System.Exception )
          {
          }
          System.Console.WriteLine("Drink: " + drink);
       } // Main
    } // class ConfigChecker
    

    流程:

    1. 研究Wbmp

    2. 首先打开X.wbmp检查详细信息

    3. 找出如何找到WBMP文件的宽度和高度(以便以后可以编写代码)。请注意,转换长度字节集合(一旦清除MSB)的最简单方法是将实体视为base-128。

    4. 查看我更新的示例代码。

    5. 我正在尝试创建一个空的Bitmap对象,并将其宽度和高度设置为我们在(3)中得出的结果

    6. 对于每一位数据,都会尝试在创建的Bitmap对象上执行SetPixel。

    7. 当WBMP宽度不是8的倍数时填充0。

    8. 使用Save()方法保存位图。

    9. 应用程序的示例用法。假设该应用程序称为Wbmp2Png。在命令行中:

      Wbmp2Png IMG_0001.wbmp IMG_0002.wbmp IMG_0003.wbmp
      

      该应用程序将IMG_0001.wbmp,IMG_0002.wbmp和IMG_0003.wbmp中的每一个转换为PNG文件。

2 个答案:

答案 0 :(得分:3)

试试这段代码吧!

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

答案 1 :(得分:3)

这似乎完成了工作。

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"));
            }
        }
    }
}