• 大小: 6.38M
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2020-12-26
  • 语言: C#
  • 标签: wpf  图像  wp    透明  

资源简介

调用外部字库、文字阴影生成图像,“百度”到处都有。
“百度”一般会告诉你一个错误的,但离正确不远的结果。
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个文件信息

评论

共有 条评论