Files
ShrlAlgoToolkit/ShrlAlgoToolkit.RevitAddins/Common/Assists/ImageAssist.cs

133 lines
5.0 KiB
C#
Raw Normal View History

2026-02-20 16:47:26 +08:00
using System.Drawing;
2025-07-11 09:20:23 +08:00
using System.Drawing.Text;
using System.Globalization;
2025-04-24 20:56:44 +08:00
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Interop;
2025-07-11 09:20:23 +08:00
using System.Windows.Media;
2025-04-24 20:56:44 +08:00
using System.Windows.Media.Imaging;
2026-02-21 16:31:24 +08:00
namespace ShrlAlgoToolkit.RevitAddins.Common.Assists;
2025-07-11 09:20:23 +08:00
2025-07-31 20:12:24 +08:00
public static class ImageAssist
2025-04-24 20:56:44 +08:00
{
/// <summary>
/// 提取资源
/// </summary>
/// <param name="resourceName"></param>
/// <param name="path"></param>
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);
}
}
2025-07-11 09:20:23 +08:00
/// <summary>
/// 资源的方式转换
///<c>var bitmap16 = ImageAssist.CreateIconBitmapSource(BoxIconFont.BxDownload, FontFamily, 16, Brushes.Black, 16, 16);</c>
///<c>var floorFinishesPbd = new PushButtonData(</c>
///<c> "房间饰面",</c>
///<c> "房间饰面",</c>
///<c> Variables.AddInPath,</c>
///<c> typeof(FloorFinishesCmd).FullName)</c>
///<c>{
///<c> Image = bitmap16,</c>
///<c> LargeImage = bitmap16,</c>
///<c>};</c>
/// </summary>
/// <param name="iconText"></param>
/// <param name="fontFamily"></param>
/// <param name="fontSize">字体大小</param>
/// <param name="foreground">前景色</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
/// <returns></returns>
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;
}
2025-04-24 20:56:44 +08:00
/// <summary>
/// 资源管理器缩略图读取
/// </summary>
/// <param name="filename"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static BitmapSource LoadFileImage(string filename, int width, int height)
{
2026-02-24 11:34:18 +08:00
var bm = WindowsThumbnailProvider.GetThumbnail(filename, width, height, ThumbnailOptions.None);
2025-07-11 09:20:23 +08:00
return Imaging.CreateBitmapSourceFromHBitmap(
bm.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
}
/// <summary>
/// 输出字体文件的方式转换
/// </summary>
/// <param name="fontPath">tff文件路径如Fonts/boxicons.tff</param>
/// <param name="unicodeChar">图标的unicode字符串</param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
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;
}
2026-02-20 15:31:44 +08:00
2025-04-24 20:56:44 +08:00
}