如何将Wbmp转换为Png?

Bic*_*c B 5 c# png image bmp

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

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

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

  1. 第一个字节是*(单色图像为0)
  2. 第二个字节称为"固定标头",为0
  3. 第三个字节是图像的宽度,以像素为单位*
  4. 第四个字节是图像的高度,以像素为单位*
  5. 按行排列的数据字节 - 每个像素一位:如果行长度不能被8整除,则该行将0填充到字节边界

我完全失去了所以任何帮助将不胜感激


其他一些代码:

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);
         }
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

流程:

  1. 对Wbmp做过研究

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

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

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

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

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

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

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

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

Wbmp2Png IMG_0001.wbmp IMG_0002.wbmp IMG_0003.wbmp
Run Code Online (Sandbox Code Playgroud)

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

Ken*_*rey 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"));
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)