C# 如何将Wbmp转换为Png?

C# 如何将Wbmp转换为Png?,c#,image,png,bmp,C#,Image,Png,Bmp,在谷歌上花了很多时间研究这个问题之后,我没有遇到一个用C语言将Wbmp图像转换成Png格式的例子# 我从互联网上下载了一些Wbmp图像,我正在使用二进制编辑器查看它们 是否有人有一个算法,将帮助我这样做,或任何代码也将帮助 到目前为止我知道的事情: 第一个字节为*(单色图像为0) 第二个字节称为“固定头”,为0 第三个字节是图像的宽度,以像素为单位* 第四个字节是图像的高度(以像素为单位)* 按行排列的数据字节–每像素一位: 如果行长度不能被8整除,则该行被0填充到0 字节边界 我完全迷路了,任

在谷歌上花了很多时间研究这个问题之后,我没有遇到一个用C语言将Wbmp图像转换成Png格式的例子# 我从互联网上下载了一些Wbmp图像,我正在使用二进制编辑器查看它们

是否有人有一个算法,将帮助我这样做,或任何代码也将帮助

到目前为止我知道的事情:

  • 第一个字节为*(单色图像为0)
  • 第二个字节称为“固定头”,为0
  • 第三个字节是图像的宽度,以像素为单位*
  • 第四个字节是图像的高度(以像素为单位)*
  • 按行排列的数据字节–每像素一位: 如果行长度不能被8整除,则该行被0填充到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);
             }
          }
       }
    }
    

    过程:

  • 对Wbmp进行了研究

  • 打开X.wbmp首先检查详细信息

  • 了解如何找到WBMP文件的宽度和高度(以便以后编写代码)。请注意,转换长度字节集合(清除MSB后)的最简单方法是将实体视为base-128

  • 看看我更新的示例代码

  • 我正在尝试创建一个空位图对象,并将其宽度和高度设置为(3)中的值

  • 对于每一位数据,将尝试在创建的位图对象上执行SetPixel

  • 当WBMP宽度不是8的倍数时,填充0

  • 使用Save()方法保存位图

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

    Wbmp2Png IMG_0001.wbmp IMG_0002.wbmp IMG_0003.wbmp
    

    应用程序会将IMG_0001.wbmp、IMG_0002.wbmp和IMG_0003.wbmp中的每一个文件转换为PNG文件。

    试试这段代码,这很有效

    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);
        }
    }
    
    使用System.IO;
    使用系统图;
    使用系统、绘图、成像;
    使用System.Runtime.InteropServices;
    类wbmp转换器
    {
    公共void转换(字符串输入文件、字符串输出文件、图像格式)
    {
    字节[]数据=File.ReadAllBytes(inputFile);
    字节tmp;
    整数宽度=0,高度=0,偏移量=2;
    做
    {
    tmp=数据[offset++];
    宽度=(宽度3;
    var tmpdata=新字节[高度*宽度];
    对于(int i=0;i>=1;
    如果(掩码==0)
    {
    掩码=0x80;
    pos++;
    }
    }
    }
    Marshal.Copy(tmpdata,0,bd.Scan0,tmpdata.Length);
    bmp.UnlockBits(bd);
    保存(输出文件,格式);
    }
    }
    
    这似乎完成了任务

    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"));
                }
            }
        }
    }
    
    使用系统图;
    使用System.IO;
    命名空间wbmp2png
    {
    班级计划
    {
    静态void Main(字符串[]参数)
    {
    foreach(args中的字符串文件)
    {
    如果(!File.Exists(File))
    {
    继续;
    }
    byte[]data=File.ReadAllBytes(文件);
    整数宽度=0;
    整数高度=0;
    int i=2;
    对于(;数据[i]>>7==1;i++)
    {
    宽度=(宽度7==1;i++)
    {
    高度=(高度(7-(x%8))&1)==1??颜色。白色:颜色。黑色);
    }
    }
    Save(Path.ChangeExtension(文件“png”);
    }
    }
    }
    }
    
    Hi,代码有错误,请发布完整的类,包括“Using.System”等语句。假设文件名为test.wbmp,其位置为Desktop。
    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"));
                }
            }
        }
    }