How to convert Wbmp to Png?

After spending a lot of time researching this on Google, I could not find an example of converting a Wbmp image to Png format in C # I downloaded some Wbmp images from the Internet and I view them using a binary editor.

Does anyone have an algorithm that will help me with this, or any code will help.

Things I know so far:

  • The first byte is type * (0 for monochrome images)
  • The second byte is called a "fixed header" and is 0
  • The third byte is the image width in pixels *
  • The fourth byte is the height of the image in pixels *
  • Data byte placed in lines - one bit per pixel: If the line length is not divisible by 8, the line has a 0-complement to the byte boundary

I am completely lost, so any help would be appreciated


Some other codes:

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

and

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

Process:

  • Wbmp

  • X.wbmp, .

  • , WBMP ( ). , ( MSB) , base-128.

  • , .

  • Bitmap , (3)

  • SetPixel Bitmap.

  • 0, WBMP 8.

  • , Save().

. , Wbmp2Png. :

Wbmp2Png IMG_0001.wbmp IMG_0002.wbmp IMG_0003.wbmp

IMG_0001.wbmp, IMG_0002.wbmp IMG_0003.wbmp PNG.

+5
2

, .

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

All Articles