Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 转换为字节数组时图像数据丢失_C#_.net_Image_Bitmap_Memorystream - Fatal编程技术网

C# 转换为字节数组时图像数据丢失

C# 转换为字节数组时图像数据丢失,c#,.net,image,bitmap,memorystream,C#,.net,Image,Bitmap,Memorystream,我试图使用内存流将图像转换为字节数组,但是,当我恢复图像时,图像看起来不同 我制作了一个简单的表单应用程序来显示问题。我在这个例子中使用google chrome图标: var process = Process.GetProcessById(3876); // found pid manually var image = Icon.ExtractAssociatedIcon(process.MainModule.FileName).ToBitmap(); pictureBox1.Image =

我试图使用
内存流
将图像转换为字节数组,但是,当我恢复图像时,图像看起来不同

我制作了一个简单的
表单
应用程序来显示问题。我在这个例子中使用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);
}
结果:

你知道我错过了什么吗


更新我能够使用以下代码获得正确的字节:

var converter = new ImageConverter();
var imageBytes = (byte[]) converter.ConvertTo(image, typeof(byte[]));
我仍然想知道内存流出了什么问题..

。当它们包含透明零件时,将转换为BMP或JPG。您也不需要
ImageConverter
它所做的几乎与您的代码在没有BMP转换的情况下所做的一样:

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

。当它们包含透明零件时,将转换为BMP或JPG。您也不需要
ImageConverter
它所做的几乎与您的代码在没有BMP转换的情况下所做的一样:

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

尝试使用32位格式,如PNG。圆圈可能需要透明。请尝试使用32位格式,如PNG。这个圈子可能需要透明度。