问题描述
花了很多时间在Google上进行研究之后,我看不到将C#将Wbmp图像转换为Png格式的示例.我已经从互联网上下载了一些Wbmp图像,并且正在使用Binary Editor查看它们.
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.
到目前为止我所知道的事情:
- 第一个字节是类型*(单色图像为0)
- 第二个字节称为固定标头",为0
- 第三字节是图像的宽度(以像素为单位)*
- 第四个字节是图像的高度(以像素为单位)*
- 数据字节按行排列–每个像素一位:如果行长度不能被8整除,则该行将0填充为字节边界
- First byte is type* (0 for monochrome images)
- Second byte is called a "fixed-header" and is 0
- Third byte is the width of the image in pixels*
- Fourth byte is the height of the image in pixels*
- Data bytes arranged in rows – one bit per pixel:Where the row length is not divisible by 8, the row is 0-padded tothe byte boundary
我完全迷路了,因此不胜感激
I am fully lost so any help will be appreciated
其他一些代码:
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
过程:
-
对Wbmp的研究
Did research on Wbmp
打开X.wbmp首先检查详细信息
Open up X.wbmp to check details first
计算出如何找到WBMP文件的宽度和高度(以便以后可以编写代码).请注意,转换长度字节的集合(一旦清除了MSB)的最简单方法是将实体视为base-128.
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.
我正在尝试创建一个空的Bitmap对象,并将其宽度和高度设置为我们在(3)中计算出的值
I am trying to create an empty Bitmap object and set its width and height to what we worked out in (3)
对于每个数据位,将尝试对创建的Bitmap对象执行SetPixel.
For every bit of data, will try and do a SetPixel on the Bitmap object created.
当WBMP宽度不是8的倍数时,补0.
Padded 0s when the WBMP width is not a multiple of 8.
使用Save()方法保存位图.
Save Bitmap using the Save() method.
该应用程序的用法示例.假定该应用程序名为Wbmp2Png.在命令行中:
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
应用程序将IMG_0001.wbmp,IMG_0002.wbmp和IMG_0003.wbmp分别转换为PNG文件.
The application converts each of IMG_0001.wbmp, IMG_0002.wbmp, and IMG_0003.wbmp to PNG files.
推荐答案
这似乎可以完成工作.
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"));
}
}
}
}
这篇关于怎么把Wbmp转换成Png?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!