首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#新手

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

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;            }        } 

热点排行