如何将字节转换为图像,将图像转换为字节,并在wpf c#中的图像框中显示

时间:2012-06-25 06:42:44

标签: asp.net wpf

我的代码将图像转换为字节,但我在图像框中出现黑屏:

 Bitmap bitmap = new Bitmap(100, 100);
 MemoryStream MemImage = new MemoryStream();
 bitmap.Save(name, ImageFormat.Bmp);
 bitmap.Save(MemImage, ImageFormat.Bmp);
 byte[] Byte = MemImage.ToArray();

将字节转换为图像:

byte[] data = (byte[])Query.Images;
MemoryStream strm = new MemoryStream();
strm.Write(data, 0, data.Length);
strm.Position = 0;
System.Drawing.Image imgTemp = System.Drawing.Image.FromStream(strm);
BitmapImage bi = new BitmapImage();
bi.BeginInit();
MemoryStream ms = new MemoryStream();
imgTemp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
ms.Seek(0, SeekOrigin.Begin);
bi.StreamSource = ms;
bi.EndInit();
ImageBox.Source = bi;

但在上面的代码中,一个问题是图像框中没有显示图像(显示黑屏) 请解决这个问题并发给我.....谢谢

1 个答案:

答案 0 :(得分:3)

试试此代码

public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
    MemoryStream ms = new MemoryStream();
    imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
    return ms.ToArray();
}
public Image byteArrayToImage(byte[] byteArrayIn)
{
    MemoryStream ms = new MemoryStream(byteArrayIn);
    Image returnImage = Image.FromStream(ms);
    return returnImage;
}