日期:2014-05-17  浏览次数:21015 次

C#新手求助
这里有一个dat格式的文件,如何编程使其在pictureBox里面显示出图像来。谢谢~~


OpenFileDialog file = new OpenFileDialog(); 
if (file.ShowDialog() == DialogResult.OK) 

//将图片读成文件流 
FileStream fs = new FileStream(file.FileName, FileMode.Open); 
byte[] b = new byte[int.Parse(fs.Length.ToString())]; 
//将文件流字节码放进数组 
fs.Read(b,0,int.Parse(fs.Length.ToString())); 
MemoryStream ms = new MemoryStream(b); 
//转为图片
Image i = Bitmap.FromStream(ms, true); 
pictureBox1.Image = i; 



这个运行到Image i = Bitmap.FromStream(ms, true);这总是说参数无效。求解答!麻烦讲清楚点,谢谢!


------解决方案--------------------
C# code

public static Image BytesToImage(byte[] bytes)
        {
            try
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    stream.Write(bytes, 0, bytes.Length);
                    Image image = Image.FromStream(stream);
                    return image;
                }
            }
            catch
            {
                return null;
            }
        }