整理代码
This commit is contained in:
142
ShrlAlgoToolkit.RevitAddins/Assists/ImageAssist.cs
Normal file
142
ShrlAlgoToolkit.RevitAddins/Assists/ImageAssist.cs
Normal file
@@ -0,0 +1,142 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Text;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Interop;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
|
||||
using ShrlAlgoToolkit.Core.Assists;
|
||||
using ShrlAlgoToolkit;
|
||||
using ShrlAlgoToolkit.Core;
|
||||
|
||||
namespace ShrlAlgoToolkit.RevitAddins.Assists;
|
||||
|
||||
public static class ImageAssist
|
||||
{
|
||||
/// <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);
|
||||
}
|
||||
}
|
||||
/// <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;
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
var bm = Assists.WindowsThumbnailProvider.GetThumbnail(filename, width, height, Assists.ThumbnailOptions.None);
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user