这可能是一个现有问题,但我得到的答案并不完全是我想要的。
在 C# Winforms 中,我想从图片框中转换图像(而不是路径)并将其转换为字节数组并在标签中显示该字节数组。
目前,这就是我所拥有的。
Byte[] result = (Byte[]) new ImageConverter().ConvertTo(pbOrigImage.Image, typeof(Byte[]));
然后,在标签中显示字节数组后,我想将其从字节数组转换为图像。我目前没有关于图像到字节数组转换的代码。但这可能吗?
答案 0 :(得分:1)
您可以使用以下方法将byte[]
转换为Image
,
public byte[] ConvertImageToBytes(Image img)
{
byte[] arr;
using (MemoryStream ms = new MemoryStream())
{
Bitmap bmp = new Bitmap(img);
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
arr = ms.ToArray();
}
return arr;
}
public Image ConvertBytesToImage(byte[] arr)
{
using (MemoryStream ms = new MemoryStream(arr))
{
return Bitmap.FromStream(ms);
}
}
要将byte[]
转换为string
或反之亦然,您可以参考this