public static IModelIconExtractor <IEntryModel> Create(IModelIconExtractor <IEntryModel> baseExtractor, Func <IEntryModel, string> keyFunc, Func <IEntryModel, string> text2DrawFunc, System.Drawing.Color color) { return(ModelIconExtractor <IEntryModel> .FromTaskFuncCachable( keyFunc, em => getIcon(baseExtractor, em, text2DrawFunc, color) )); }
public static async Task <ImageSource> GetIconForModelAsync(this IModelIconExtractor <IEntryModel> extractor, IEntryModel model, CancellationToken ct) { byte[] bytes = await extractor.GetIconBytesForModelAsync(model, ct); ct.ThrowIfCancellationRequested(); return(bytes == null ? new BitmapImage() : FileExplorer.WPF.Utils.BitmapSourceUtils.CreateBitmapSourceFromBitmap(bytes)); }
private static async Task <byte[]> getIcon(IModelIconExtractor <IEntryModel> baseExtractor, IEntryModel em, Func <IEntryModel, string> text2DrawFunc, System.Drawing.Color color) { if (em != null && !String.IsNullOrEmpty(em.FullPath)) { byte[] baseBytes = await baseExtractor.GetIconBytesForModelAsync(em, CancellationToken.None); Bitmap baseBitmap = new Bitmap(new MemoryStream(baseBytes)); using (Graphics g = Graphics.FromImage(baseBitmap)) { g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; string text = text2DrawFunc(em); Font font = new Font("Comic Sans MS", Math.Max(baseBitmap.Width / 5, 1), System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic); float height = g.MeasureString(text, font).Height; float rightOffset = baseBitmap.Width / 5; //if (size == IconSize.small) //{ // font = new Font("Arial", 5, System.Drawing.FontStyle.Bold); // height = g.MeasureString(ext, font).Height; // rightOffset = 0; //} g.DrawString(text, font, new System.Drawing.SolidBrush(color), new RectangleF(0, baseBitmap.Height - height, baseBitmap.Width - rightOffset, height), new StringFormat(StringFormatFlags.DirectionRightToLeft)); return(baseBitmap.ToByteArray()); } } return(new byte[] { }); }