2012-08-13 20 views
5

Después de pasar mucho tiempo investigando sobre esto en Google, no pude encontrar un ejemplo de conversión de una imagen Wbmp a formato Png en C# He descargado algunas imágenes Wbmp de Internet y las estoy viendo con un editor binario.Cómo convertir Wbmp a Png?

¿Alguien tiene un algoritmo que me ayude a hacerlo o cualquier código también ayudará.

cosas que sé hasta ahora:

  1. primer byte es el tipo * (0 para imágenes monocromas)
  2. Segundo byte se llama una “cabecera fija” y es 0
  3. Tercer byte es el ancho de la imagen en píxeles *
  4. Cuarto byte es el alto de la imagen en píxeles *
  5. Bytes de datos ordenados en filas: un bit por píxel: Cuando la longitud de la fila no es divisible por 8, la fila es 0-acolchado para el límite de byte

estoy totalmente perdido por lo que se observará ninguna ayuda


Algunos de los otros códigos :

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

Y

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 

proceso:

  1. investigó sobre wbmp

  2. Abre X.wbmp a comprobar los detalles primeros

  3. entender cómo se puede encontrar la anchura y la altura del archivo WBMP (de modo que se puede luego escribir el código). Tenga en cuenta que la forma más sencilla de convertir una colección de bytes de longitud (una vez que se borre el MSB) es tratar a la entidad como base-128.

  4. Mire el código de muestra que actualicé.

  5. Estoy tratando de crear un objeto de mapa de bits vacío y establecer su anchura y la altura de lo que funcionó en (3)

  6. Para cada bit de datos, a tratar de hacer un SetPixel en el objeto de mapa de bits creado.

  7. 0s acolchados cuando la anchura WBMP no es un múltiplo de 8.

  8. Guardar Bitmap utilizando el método Save().

Ejemplo de uso de la aplicación. Se supone que la aplicación se llama Wbmp2Png. En la línea de comandos:

Wbmp2Png IMG_0001.wbmp IMG_0002.wbmp IMG_0003.wbmp 

La aplicación convierte cada uno de IMG_0001.wbmp, IMG_0002.wbmp e IMG_0003.wbmp a archivos PNG.

Respuesta

3

Esto parece hacer el trabajo.

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

Pruebe este código, ¡esto funciona!

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

Hola, el código está dando errores, publique su clase completa incluyendo las declaraciones "Using.System" etc. Suponga que el archivo se llama test.wbmp y su ubicación es Desktop. –

+0

@AmberArroway: actualizado – Ria

+0

eche un vistazo a la pregunta editada –