2016-07-05 6 views
0

MemoryStreamを使用してイメージをバイト配列に変換しようとしていますが、イメージを復元するとイメージが異なって見えます。バイト配列に変換すると画像データが失われる

単純なFormアプリを作成して問題を表示しました。私は、この例のGoogle Chromeアイコンを使用しています:

var process = Process.GetProcessById(3876); // found pid manually 
var image = Icon.ExtractAssociatedIcon(process.MainModule.FileName).ToBitmap(); 
pictureBox1.Image = image; 

byte[] imageBytes; 
using (var ms = new MemoryStream()) 
{ 
    image.Save(ms, ImageFormat.Bmp); 
    imageBytes = ms.ToArray(); 
} 

using (var ms = new MemoryStream(imageBytes)) 
{ 
    pictureBox2.Image = (Bitmap) Image.FromStream(ms); 
} 

結果:私はここに欠けているものを

image before and image after

任意のアイデアを?私は次のコードを使用して、適切なバイトを取得することができました


更新

var converter = new ImageConverter(); 
var imageBytes = (byte[]) converter.ConvertTo(image, typeof(byte[])); 

はまだいただきましメモリストリームとアップけれども..

+0

は、PNGなどの32ビット形式を使用してみてください:あなたはまた、あなたのコードは、BMP変換せずに行い、ほぼ何をやっているImageConverterを必要といけません。サークルはおそらく透明性が必要です。 –

答えて

3

Icons are complicatedを知っていただきたいと思います。 BMPやJPG almost always seems to end badlyに変換する透明部分が含まれている場合。

var process = Process.GetProcessById(844); // found pid manually 
var image = Icon.ExtractAssociatedIcon(process.MainModule.FileName).ToBitmap(); 
pb1.Image = image; 

byte[] imageBytes; 

using (var ms = new MemoryStream()) 
{ 
    image.Save(ms, ImageFormat.Png);  // PNG for transparency 
    ms.Position = 0; 
    pb2.Image = (Bitmap)Image.FromStream(ms);     
} 

ImageConverter Reference Source

関連する問題