Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Wbmp to Png?

Tags:

c#

image

png

bmp

After spending much time researching about this on Google, I could not come across an example of converting a Wbmp image to Png format in C# I have download some Wbmp images from the internet and I am viewing them using a Binary Editor.

Does anyone have an algorithm that will assist me in doing so or any code will also help.

Things I know so far:

  1. First byte is type* (0 for monochrome images)
  2. Second byte is called a “fixed-header” and is 0
  3. Third byte is the width of the image in pixels*
  4. Fourth byte is the height of the image in pixels*
  5. Data bytes arranged in rows – one bit per pixel: Where the row length is not divisible by 8, the row is 0-padded to the byte boundary

I am fully lost so any help will be appreciated


Some of the other code:

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 :

  1. Did research on Wbmp

  2. Open up X.wbmp to check details first

  3. Work out how you find the width and height of the WBMP file (so that you can later write the code). Note that the simplest way to convert a collection of length bytes (once the MSB is cleared) is to treat the entity as base-128.

  4. Look at sample code I updated.

  5. I am trying to create an empty Bitmap object and set its width and height to what we worked out in (3)

  6. For every bit of data, will try and do a SetPixel on the Bitmap object created.

  7. Padded 0s when the WBMP width is not a multiple of 8.

  8. Save Bitmap using the Save() method.

Example usage of the application. It is assumed that the application is called Wbmp2Png. In command line:

Wbmp2Png IMG_0001.wbmp IMG_0002.wbmp IMG_0003.wbmp

The application converts each of IMG_0001.wbmp, IMG_0002.wbmp, and IMG_0003.wbmp to PNG files.

like image 712
Bic B Avatar asked Aug 13 '12 02:08

Bic B


Video Answer


2 Answers

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"));
            }
        }
    }
}
like image 52
Kendall Frey Avatar answered Oct 20 '22 00:10

Kendall Frey


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);
    }
}
like image 38
Ria Avatar answered Oct 19 '22 23:10

Ria