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。这个圈子可能需要透明度。