如何将Wbmp转换为Png?

5
经过在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);
         }
      }
   }
}

并且
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最简单的方法是将实体视为base-128。

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

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

  6. 对于每个数据位,将尝试在所创建的Bitmap对象上执行SetPixel操作。

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

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

应用程序的用法示例。假定该应用程序名为Wbmp2Png。在命令行中:

Wbmp2Png IMG_0001.wbmp IMG_0002.wbmp IMG_0003.wbmp

该应用程序将IMG_0001.wbmp、IMG_0002.wbmp和IMG_0003.wbmp转换为PNG文件。
2个回答

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

嗨,代码出现错误,请发布包括“Using.System”等语句的完整类。假设文件名为test.wbmp,位置在桌面上。 - Bic B

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接