using System.Drawing; using System.Drawing.Text; using System.Globalization; using System.IO; using System.Reflection; using System.Windows; using System.Windows.Interop; using System.Windows.Media; using System.Windows.Media.Imaging; namespace ShrlAlgoToolkit.RevitAddins.Common.Assists; public static class ImageAssist { /// /// 提取资源 /// /// /// public static void ExtractResource(string resourceName, string path) { using var manifestResourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName); using var stream = File.Create(path); var array = new byte[8192]; int count; while ((count = manifestResourceStream.Read(array, 0, array.Length)) > 0) { stream.Write(array, 0, count); } } /// /// 资源的方式转换 ///var bitmap16 = ImageAssist.CreateIconBitmapSource(BoxIconFont.BxDownload, FontFamily, 16, Brushes.Black, 16, 16); ///var floorFinishesPbd = new PushButtonData( /// "房间饰面", /// "房间饰面", /// Variables.AddInPath, /// typeof(FloorFinishesCmd).FullName) ///{ /// Image = bitmap16, /// LargeImage = bitmap16, ///}; /// /// /// /// 字体大小 /// 前景色 /// 宽度 /// 高度 /// public static BitmapSource CreateIconBitmapSource(string iconText, System.Windows.Media.FontFamily fontFamily, double fontSize, System.Windows.Media.Brush foreground, double width, double height) { var formattedText = new FormattedText( iconText, CultureInfo.InvariantCulture, FlowDirection.LeftToRight, new Typeface(fontFamily, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal), fontSize, foreground, 1.0); var drawingVisual = new DrawingVisual(); using (var drawingContext = drawingVisual.RenderOpen()) { var x = (width - formattedText.Width) / 2; var y = (height - formattedText.Height) / 2; drawingContext.DrawText(formattedText, new System.Windows.Point(x, y)); } var renderTarget = new RenderTargetBitmap((int)Math.Ceiling(width), (int)Math.Ceiling(height), 96, 96, PixelFormats.Pbgra32); renderTarget.Render(drawingVisual); return renderTarget; } /// /// 资源管理器缩略图读取 /// /// /// /// /// public static BitmapSource LoadFileImage(string filename, int width, int height) { var bm = WindowsThumbnailProvider.GetThumbnail(filename, width, height, ThumbnailOptions.None); return Imaging.CreateBitmapSourceFromHBitmap( bm.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); } /// /// 输出字体文件的方式转换 /// /// tff文件路径,如Fonts/boxicons.tff /// 图标的unicode字符串 /// /// /// public static Bitmap RenderIconFontToBitmap(string fontPath, string unicodeChar, int width, int height) { // 加载字体 PrivateFontCollection fontCollection = new PrivateFontCollection(); fontCollection.AddFontFile(fontPath); System.Drawing.FontFamily fontFamily = fontCollection.Families[0]; // 创建位图 Bitmap bitmap = new Bitmap(width, height); using (Graphics g = Graphics.FromImage(bitmap)) { g.Clear(System.Drawing.Color.Transparent); using (Font font = new Font(fontFamily, Math.Min(width, height) / 2)) { g.TextRenderingHint = TextRenderingHint.AntiAlias; StringFormat format = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }; g.DrawString(unicodeChar, font, System.Drawing.Brushes.Black, new RectangleF(0, 0, width, height), format); } } return bitmap; } }