2016-07-11 2 views
0

は、私は彼らはあなたが何を問題となっていると思いますC#でビットマップを1Dバイト配列に変換する方法とその逆を行う方法はありますか?

enter image description here

、動作していないように見える、

public static byte[] BitmapToByteArray(Bitmap image) 
     { 
      byte[] returns = null; 
      if (image.PixelFormat == PixelFormat.Format8bppIndexed) 
      { 
       BitmapData bitmapData = image.LockBits(
               new Rectangle(0, 0, image.Width, image.Height), 
               ImageLockMode.ReadWrite, 
               image.PixelFormat); 
       int noOfPixels = image.Width * image.Height; 
       int colorDepth = Bitmap.GetPixelFormatSize(image.PixelFormat); 
       int step = colorDepth/8; 
       byte[] bytes = new byte[noOfPixels * step]; 
       IntPtr address = bitmapData.Scan0; 
       Marshal.Copy(address, bytes, 0, bytes.Length); 
       //////////////////////////////////////////////// 
       /// 
       returns = (byte[])bytes.Clone(); 
       /// 
       //////////////////////////////////////////////// 
       Marshal.Copy(bytes, 0, address, bytes.Length); 
       image.UnlockBits(bitmapData); 
      } 
      else 
      { 
       throw new Exception("8bpp indexed image required"); 
      } 
      return returns; 
     } 

そして、

public static Bitmap ByteArrayToBitmap(byte[] bytes, int width, int height, PixelFormat pixelFormat) 
     { 
      Bitmap bitmap = new Bitmap(width, height, pixelFormat); 
      BitmapData bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat); 
      int colorDepth = Bitmap.GetPixelFormatSize(pixelFormat); 
      int noOfChannels = colorDepth/8; 
      IntPtr address = bitmapData.Scan0; 
      ////////////////////////////////////////////////////////////// 
      // 
      Marshal.Copy(bytes, 0, address, width * height * noOfChannels); 
      // 
      ////////////////////////////////////////////////////////////// 
      bitmap.UnlockBits(bitmapData); 

      return bitmap; 
     } 

を、以下の方法を書きましたか?

N.B.

ドライバプログラム、

public class MainClass 
{ 
    public static void Main(string [] args) 
    { 
     Bitmap inputBmp = (Bitmap)Bitmap.FromFile(@"cameraman.gif"); 

     byte[] bytes = Converter.BitmapToByteArray(inputBmp);//byte[65536] 

     Bitmap outputBmp = Converter.ByteArrayToBitmap(bytes, inputBmp.Width, inputBmp.Height, PixelFormat.Format8bppIndexed); 

     PictureDisplayForm f = new PictureDisplayForm(inputBmp, outputBmp); 
     f.ShowDialog(); 
    } 
} 
+2

ファイルヘッダー、ビットマップヘッダー、およびパレットも含まれています。必要に応じて、BMPファイル形式。私は "例外"が何を意味するのか推測したくないですが、この余分なデータを画像ピクセルとして解釈すると間違っているかもしれません。 –

+0

2番目の方法は、生のファイルをバッファにダンプします。これは、ビットマップを生成するために解釈される必要があります。 –

答えて

1

わかりました。

私はそれを解決しました。

public static byte[] BitmapToByteArray(Bitmap image) 
     { 
      byte[] returns = null; 
      if (image.PixelFormat == PixelFormat.Format8bppIndexed) 
      { 
       BitmapData bitmapData = image.LockBits(
               new Rectangle(0, 0, image.Width, image.Height), 
               ImageLockMode.ReadWrite, 
               image.PixelFormat); 
       int noOfPixels = image.Width * image.Height; 
       int colorDepth = Bitmap.GetPixelFormatSize(image.PixelFormat); 
       int step = colorDepth/8; 
       byte[] bytes = new byte[noOfPixels * step]; 
       IntPtr address = bitmapData.Scan0; 
       Marshal.Copy(address, bytes, 0, bytes.Length); 
       //////////////////////////////////////////////// 
       /// 
       returns = (byte[])bytes.Clone(); 
       /// 
       //////////////////////////////////////////////// 
       Marshal.Copy(bytes, 0, address, bytes.Length); 
       image.UnlockBits(bitmapData); 
      } 
      else 
      { 
       throw new Exception("8bpp indexed image required"); 
      } 
      return returns; 
     } 

     public static Bitmap ByteArray1dToBitmap(byte[] bytes, int width, int height) 
     { 
      PixelFormat pixelFormat = PixelFormat.Format8bppIndexed; 
      Bitmap bitmap = new Bitmap(width, height, pixelFormat); 

      // Set the palette for gray shades 
      ColorPalette pal = bitmap.Palette; 
      for (int i = 0; i < pal.Entries.Length; i++) 
      { 
       pal.Entries[i] = Color.FromArgb(i, i, i); 
      } 
      bitmap.Palette = pal; 

      BitmapData bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, pixelFormat); 
      int colorDepth = Bitmap.GetPixelFormatSize(pixelFormat); 
      int noOfChannels = colorDepth/8; 

      unsafe 
      { 
       byte* address = (byte*)bitmapData.Scan0; 
       int area = width * height; 
       int size = area * noOfChannels; 
       for (int i = 0; i < area; i++) 
       { 
        address[i] = bytes[i];//262144 bytes 
       } 
      } 

      ////////////////////////////////////////////////////////////// 
      bitmap.UnlockBits(bitmapData); 

      return bitmap; 
     } 
+0

あなたは本当にストライドを考慮する必要があります...保存されたイメージが圧縮されてその行を4バイトに整列させない場合、混乱するかもしれません。また、あらかじめ色深度を8で割り算しているため、そこにある計算方法は4bppまたは1bppのインデックス付きイメージでは機能しません。 – Nyerguds

関連する問題