2016-12-18 10 views
0

バイト配列をビットマップに変換するのに少し問題があります。ここに私の例外です:c#Byte to Image ArgumentOutOfRangeException

は 'System.ArgumentOutOfRangeExceptionが' タイプの未処理の例外がmscorlib.dllがで発生した

マイコード:私はシリアル化されたバイトをロードする必要が

public static System.Drawing.Bitmap ByteToImage(byte[] data) 
{ 
    System.Drawing.Bitmap bmp; 
    using (var ms = new MemoryStream(data)) 
    { 
     bmp = new System.Drawing.Bitmap(ms); 
    } 
    return bmp; 
} 

Bitmap b = ByteToImage(editor1.system.Tiles[0].ImageData); 
Form f = new Form(); 
f.BackgroundImage = b; 
f.Show(); 

配列上に配列を置き、実行時にイメージに変換します。

私はビットマップ

b.Save(@"C:\test.png"); 

を保存する場合それは私が実行時にビットマップをロードしようとした場合、私はこのエラーを取得する動作します。

+0

どのようにあなたのバイトを得るのですか?あなたはそれを示さなかった。 – Happypig375

+0

editor1とは何ですか?システムとは何ですか? – Happypig375

+0

@ Happypig375 editor1は私のuserControlです。システムはすべての要素をシリアライズするためのライブラリであり、リスト、構造体などを含みます。 –

答えて

1

次のコードを使用すると、問題が解決されます。

public static Image FormatImage(Image img, int outputWidth, int outputHeight) 
    { 

     Bitmap outputImage =null; 
     Graphics graphics = null; 
     try 
     { 
      outputImage = new Bitmap(outputWidth, outputHeight, System.Drawing.Imaging.PixelFormat.Format16bppRgb555); 
      graphics = Graphics.FromImage(outputImage); 
      graphics.DrawImage(img, new Rectangle(0, 0, outputWidth, outputHeight), 
      new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel); 

      return outputImage; 
     } 
     catch (Exception ex) 
     {     
      return img; 
     } 

}