0

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

 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;

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

4

1 回答 1

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;
}
于 2012-06-25T08:34:31.670 回答