public static void loadFaceSyncxxx(string face, RequestPicEvent callBack) { bool isFaceExit = false; foreach (var item in texturesList) { if (item == null) { Debug.Print("item为空"); callBack(null); return; } if (face == null) { Debug.Print("face为空"); callBack(null); return; } if (item.EndsWith(face)) { isFaceExit = true; break; } } if (isFaceExit) { // Debug.Print("图片存在,无需下载" + face); loadFaceFromLocal(face, callBack); } else { // Debug.Print("图片不存在,网络下载"+ face); loadFaceFromNet(face, callBack); } }
static Image loadFaceFromLocalThread(string url, RequestPicEvent callBack) { FileStream fs = null; try { fs = File.OpenRead(url); int filelength = 0; filelength = (int)fs.Length; //获得文件长度 Byte[] image = new Byte[filelength]; //建立一个字节数组 fs.Read(image, 0, filelength); //按字节流读取 Image result = Image.FromStream(fs); fs.Close(); return(result); } catch (Exception err) { Debug.Print("loadFaceFromLocalThread下载图片失败,转为网络下载。" + url + err.ToString()); int count = url.LastIndexOf("/"); string face = url.Substring(count); loadFaceFromNet(face, callBack); return(null); } finally { if (fs != null) { fs.Close(); } } }
static void loadFaceFromNet(string face, RequestPicEvent callBack) { Image tempImage; Func <string, Image> reqPic = loadFaceFromNetThread; IAsyncResult iar = reqPic.BeginInvoke(face, ar => { tempImage = reqPic.EndInvoke(ar); if (callBack != null) { callBack(tempImage); } }, reqPic); }
static void loadFaceFromLocal(string face, RequestPicEvent callBack) { string url = AppConst.WinPicPath + face; Image tempImage; Func <string, RequestPicEvent, Image> reqPic = loadFaceFromLocalThread; IAsyncResult iar = reqPic.BeginInvoke(url, callBack, ar => { tempImage = reqPic.EndInvoke(ar); if (callBack != null) { callBack(tempImage); } }, reqPic); }