private static WriteableBitmap GetWriteableBitmapFromFile(string f) { var icon = GIconHelper.GetFileIcon(f, false).ToBitmap(); IntPtr hBitmap = icon.GetHbitmap(); BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); bitmapSource.Freeze(); WriteableBitmap writeableBmp = new WriteableBitmap(bitmapSource); icon.Dispose(); return(writeableBmp); }
private static BitmapSource GetBitmapFromFileNoCache(string f, int level) { Logger.I("GetBitmapFromFileNoCache for {0} level {1}", f, level); if (level > 5) { return(null); //防止递归调用死循环,将堆栈耗尽 } if (PathHelper.IsValidWebLink(f)) { return(GetBitmapFromFileNoCache(CfgPath.Res_HTTP_Path, level + 1)); } else if (PathHelper.IsValidFS(f)) { try { Logger.I(" isvalidfs try GetBitmapFromFileNoCache for {0} level {1}", f, level); Icon ic = GIconHelper.GetFileIcon(f, false); if (ic == null) { Logger.I("GetBitmapFromFileNoCache failed: ic==null for {0} level {1}", f, level); return(null); } var icon = ic.ToBitmap(); IntPtr hBitmap = icon.GetHbitmap(); BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); bitmapSource.Freeze(); icon.Dispose(); return(bitmapSource); } catch (Exception e) { Logger.E(e); Logger.E("get icon failed!======{0}", f); if (f != CfgPath.Res_UNKNOW_Path) { return(GetBitmapFromFileNoCache(CfgPath.Res_UNKNOW_Path, level + 1)); } else { return(null); } } } else { return(GetBitmapFromFileNoCache(CfgPath.Res_UNKNOW_Path, level + 1)); } }