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:
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 :
Did research on Wbmp
Open up X.wbmp to check details first
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.
Look at sample code I updated.
I am trying to create an empty Bitmap object and set its width and height to what we worked out in (3)
For every bit of data, will try and do a SetPixel on the Bitmap object created.
Padded 0s when the WBMP width is not a multiple of 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.
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"));
}
}
}
}
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With