資源簡介
調用外部字庫、文字陰影生成圖像,“百度”到處都有。
“百度”一般會告訴你一個錯誤的,但離正確不遠的結果。
1、調用外部字庫,搜索結果是放入資源文件中,一個字庫7、8兆,放上幾個,那exe得多大,
在一些博客上找到了方法,是錯的,已在評論中糾正。
2、文字陰影可以轉為圖像,但組件須已顯示,比如:
一副圖像加上水印文字,DrawText可以直接畫,但畫模糊陰影文字無法畫上去。
“百度”一般會告訴你一個錯誤的,但離正確不遠的結果。
1、調用外部字庫,搜索結果是放入資源文件中,一個字庫7、8兆,放上幾個,那exe得多大,
在一些博客上找到了方法,是錯的,已在評論中糾正。
2、文字陰影可以轉為圖像,但組件須已顯示,比如:
一副圖像加上水印文字,DrawText可以直接畫,但畫模糊陰影文字無法畫上去。
本例根據圖像大小,畫文字自動換行顯示,原理是將文字生成背景透明的陰影圖像,再畫到圖像上去,生成1幅圖像。
如圖像Dpi不為96,調整文字大小或自行修改代碼適應。
/// <summary> /// 組件生成圖片 /// </summary> /// <param name="control">組件名稱</param> /// <param name="filename">保存路徑</param> private void SaveControlImage(FrameworkElement control, string filename) { Rect rect = VisualTreeHelper.GetDescendantBounds(control); DrawingVisual dv = new DrawingVisual(); using (DrawingContext ctx = dv.RenderOpen()) { VisualBrush brush = new VisualBrush(control); ctx.DrawRectangle(brush, null, new Rect(rect.Size)); } int width = (int)control.ActualWidth; int height = (int)control.ActualHeight; RenderTargetBitmap rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32); rtb.Render(dv); PngBitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(rtb)); using (FileStream fs = new FileStream(filename,FileMode.Create, FileAccess.Write, FileShare.None)) { encoder.Save(fs); } } /// <summary> /// 生成背景透明陰影文字圖像 /// </summary> /// <param name="content">文字內容</param> /// <param name="fontFace">字體</param> /// <param name="fontSize">字體大小</param> /// <param name="fontColor">文字顏色</param> /// <param name="textWidth">文字范圍寬度</param> /// <param name="textHeight">文字范圍高度</param> /// <param name="_dpiX">點數</param> /// <param name="_dpiY">點數</param> /// <param name="shodowOffset">陰影距離</param> /// <param name="_radius">模糊值</param> /// <param name="_opacity">陰影透明度</param> /// <param name="beginColor">文字漸變色起始顏色</param> /// <param name="endColor">文字漸變色結束顏色</param> /// <returns></returns> public static RenderTargetBitmap DrawShadowText( string content, string fontFace, double fontSize, Brush fontColor, int textWidth, int textHeight, int _dpiX, int _dpiY, Point shodowOffset, int _radius, double _opacity, Color beginColor, Color endColor ) { var visualShadow = new DrawingVisual(); using (DrawingContext drawingShadowContext = visualShadow.RenderOpen()) { var pixels = new byte[textWidth * textHeight * 4]; var maxWH = textWidth > textHeight ? textWidth : textHeight; BitmapSource bitmapSource = BitmapSource.Create(textWidth, textHeight, _dpiX, _dpiY, PixelFormats.Pbgra32, null, pixels, maxWH * 4); drawingShadowContext.DrawImage(bitmapSource, new Rect(0, 0, textWidth, textHeight)); FormattedText formatshadow = new FormattedText(content, CultureInfo.InvariantCulture, FlowDirection.LeftToRight, new Typeface(fontFace), fontSize, Brushes.Black); formatshadow.MaxTextWidth = textWidth; formatshadow.MaxTextHeight = textHeight; formatshadow.SetFontWeight(FontWeights.Normal, 0, content.Length); drawingShadowContext.DrawText(formatshadow, shodowOffset); } var image = new DrawingImage(visualShadow.Drawing); Rectangle rct = new Rectangle() { Fill = new ImageBrush(image), Effect = new BlurEffect() { Radius = _radius }, Opacity = _opacity, }; Size sz = new Size(image.Width, image.Height); rct.Measure(sz); rct.Arrange(new Rect(sz)); RenderTargetBitmap rtbmp = new RenderTargetBitmap(textWidth, textHeight, _dpiX, _dpiY, PixelFormats.Default); rtbmp.Render(rct); BitmapSource bgImage = rtbmp; RenderTargetBitmap composeImage = new RenderTargetBitmap(bgImage.PixelWidth, bgImage.PixelHeight, _dpiX, _dpiY, PixelFormats.Default); DrawingVisual drawingVisual = new DrawingVisual(); FormattedText formattext = new FormattedText(content, CultureInfo.InvariantCulture, FlowDirection.LeftToRight, new Typeface(fontFace), fontSize, fontColor); formattext.MaxTextWidth = textWidth; formattext.MaxTextHeight = textHeight; formattext.SetFontWeight(FontWeights.Normal, 0, content.Length); formattext.SetForegroundBrush( new LinearGradientBrush( beginColor, endColor, fontSize), 0, content.Length); using (DrawingContext drawingContext = drawingVisual.RenderOpen()) { drawingContext.DrawImage(bgImage, new Rect(0, 0, bgImage.Width, bgImage.Height)); drawingContext.DrawText(formattext, new Point(0, 0)); } composeImage.Render(drawingVisual); return composeImage; }
代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Configuration;
using?System.Data;
using?System.Linq;
using?System.Threading.Tasks;
using?System.Windows;
namespace?DrawText
{
????///?
????///?App.xaml?的交互邏輯
????///?
????public?partial?class?App?:?Application
????{
????}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
????..A..H.?????54272??2018-04-18?16:16??DrawText\.vs\DrawText\v15\.suo
?????文件??????????0??2018-04-18?12:55??DrawText\.vs\DrawText\v15\Server\sqlite3\db.lock
?????文件?????688128??2018-04-18?16:16??DrawText\.vs\DrawText\v15\Server\sqlite3\storage.ide
?????文件????????189??2018-04-18?12:55??DrawText\DrawText\App.config
?????文件????????370??2018-04-18?12:55??DrawText\DrawText\App.xaml
?????文件????????335??2018-04-18?12:55??DrawText\DrawText\App.xaml.cs
?????文件??????28262??2018-04-16?13:43??DrawText\DrawText\bin\Debug\2.jpg
?????文件??????14336??2018-04-18?15:57??DrawText\DrawText\bin\Debug\DrawText.exe
?????文件????????189??2018-04-18?12:55??DrawText\DrawText\bin\Debug\DrawText.exe.config
?????文件??????24064??2018-04-18?15:57??DrawText\DrawText\bin\Debug\DrawText.pdb
?????文件??????65400??2018-04-18?15:03??DrawText\DrawText\bin\Debug\u.jpg
?????文件???10828284??2012-04-26?09:53??DrawText\DrawText\bin\Debug\中文字體.ttf
?????文件?????332434??2018-04-18?15:56??DrawText\DrawText\bin\Debug\水印圖像.png
?????文件?????242623??2018-04-18?15:24??DrawText\DrawText\bin\Debug\水印圖像1.png
?????文件???????4138??2018-04-18?12:55??DrawText\DrawText\DrawText.csproj
?????文件????????932??2018-04-18?14:37??DrawText\DrawText\MainWindow.xaml
?????文件???????8772??2018-04-18?15:56??DrawText\DrawText\MainWindow.xaml.cs
?????文件???????2310??2018-04-18?12:55??DrawText\DrawText\Properties\AssemblyInfo.cs
?????文件???????2829??2018-04-18?12:55??DrawText\DrawText\Properties\Resources.Designer.cs
?????文件???????5612??2018-04-18?12:55??DrawText\DrawText\Properties\Resources.resx
?????文件???????1095??2018-04-18?12:55??DrawText\DrawText\Properties\Settings.Designer.cs
?????文件????????201??2018-04-18?12:55??DrawText\DrawText\Properties\Settings.settings
?????文件???????1123??2018-04-18?12:55??DrawText\DrawText.sln
?????目錄??????????0??2018-04-18?16:16??DrawText\.vs\DrawText\v15\Server\sqlite3
?????目錄??????????0??2018-04-18?12:55??DrawText\.vs\DrawText\v15\Server
?????目錄??????????0??2018-04-18?12:55??DrawText\.vs\DrawText\v15
?????目錄??????????0??2018-04-18?16:16??DrawText\DrawText\bin\Debug
?????目錄??????????0??2018-04-18?16:12??DrawText\DrawText\obj\Debug
?????目錄??????????0??2018-04-18?12:55??DrawText\.vs\DrawText
?????目錄??????????0??2018-04-18?12:55??DrawText\DrawText\bin
............此處省略8個文件信息
- 上一篇:C# 直角坐標系(繪圖)
- 下一篇:c#數字形態學圖像處理
評論
共有 條評論