2012-08-13 45 views
5

在Google上花了很多時間研究這個問題之後,我找不到在C#中將Wbmp圖像轉換爲Png格式的示例。我從互聯網上下載了一些Wbmp圖像,並使用二進制編輯器查看它們。如何把Wbmp轉換成Png?

有沒有人有一個算法,這將有助於我這樣做或任何代碼也將有所幫助。

事情我知道,到目前爲止:

  1. 第一個字節是類型*(0單色圖像)
  2. 第二個字節被稱爲「固定頭」,並且是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. 我試圖創建一個空的位圖對象,其寬度和高度設置爲我們在(3)

  6. 制定了數據的每一位,會盡量做到位圖對象的SetPixel創建。

  7. 軟墊0時WBMP寬度不使用保存()方法8

  8. 保存位圖的倍數。

應用程序的用法示例。假定該應用程序被稱爲Wbmp2Png。在命令行中:

Wbmp2Png IMG_0001.wbmp IMG_0002.wbmp IMG_0003.wbmp 

該應用程序轉換每個IMG_0001.wbmp,IMG_0002。wbmp和IMG_0003.wbmp轉換爲PNG文件。

回答

3

這似乎完成了工作。

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

試試這個代碼,這個工程!

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

嗨,代碼是給出錯誤,發佈你的完整的類,包括「Using.System」等聲明。假定該文件名爲test.wbmp,其位置是Desktop。 – 2012-08-13 04:23:14

+0

@AmberArroway:更新 – Ria 2012-08-13 04:32:08

+0

看看編輯過的問題 – 2012-08-13 04:39:33