【问题标题】:How to convert Wbmp to Png?如何将 Wbmp 转换为 PNG?
【发布时间】:2012-08-09 06:35:02
【问题描述】:

在 Google 上花了很多时间研究这个问题后,我找不到在 C# 中将 Wbmp 图像转换为 Png 格式的示例 我从互联网上下载了一些 Wbmp 图像,我正在使用二进制编辑器查看它们。

有没有人有算法可以帮助我这样做,或者任何代码也会有帮助。

到目前为止我所知道的:

  1. 第一个字节是类型*(单色图像为 0)
  2. 第二个字节称为“fixed-header”,为 0
  3. 第三个字节是图像的宽度,以像素为单位*
  4. 第四个字节是图像的高度,以像素为单位*
  5. 按行排列的数据字节 - 每个像素一位: 在行长度不能被 8 整除的情况下,该行被 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);
         }
      }
   }
}

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

流程:

  1. 做过Wbmp研究

  2. 先打开X.wbmp查看详情

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

  4. 查看我更新的示例代码。

  5. 我正在尝试创建一个空的 Bitmap 对象并将其宽度和高度设置为我们在 (3) 中计算出的值

  6. 对于每一位数据,将尝试在创建的 Bitmap 对象上执行 SetPixel。

  7. 当 WBMP 宽度不是 8 的倍数时填充 0。

  8. 使用 Save() 方法保存位图。

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

Wbmp2Png IMG_0001.wbmp IMG_0002.wbmp IMG_0003.wbmp

应用程序将 IMG_0001.wbmp、IMG_0002.wbmp 和 IMG_0003.wbmp 分别转换为 PNG 文件。

【问题讨论】:

    标签: c# image png bmp


    【解决方案1】:

    这似乎完成了工作。

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

    【讨论】:

      【解决方案2】:

      试试这个代码,这行得通!

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

      【讨论】:

      • 您好,代码出错,请发布您的完整课程,包括“Using.System”等语句。假设文件名为 test.wbmp,其位置为 Desktop。
      猜你喜欢
      • 1970-01-01
      • 2012-07-10
      • 2018-10-11
      • 2011-04-19
      • 1970-01-01
      • 2022-10-05
      • 2013-10-04
      • 2015-01-31
      • 2021-10-14
      相关资源
      最近更新 更多