1.根据路径把图片转成二进制
1 public Byte[] ReadImageFile(string path) 2 { 3 FileStream fs = File.OpenRead(path); //OpenRead 4 int filelength = 0; 5 filelength = (int)fs.Length; //获得文件长度 6 Byte[] imgByte = new Byte[filelength]; //建立一个字节数组 7 fs.Read(imgByte, 0, filelength); //按字节流读取 8 System.Drawing.Image result = System.Drawing.Image.FromStream(fs); 9 fs.Close(); 10 return imgByte; 11 }
2.把二进制文件转成图片
System.IO.MemoryStream ms = new System.IO.MemoryStream(imgByte); System.Drawing.Image img = System.Drawing.Image.FromStream(ms); this.picBoxImage.Image = img;
3.在异步线程修改主线程UI(线程间操作无效,从不是创建控件"XX"线程访问他),有两个方法,
①在Load方法中增加 Control.CheckForIllegalCrossThreadCalls = false;
②使用Invoke
this.Invoke(new MethodInvoker(delegate() { this.picBoxImage.Image = bit; }));