如何在SharpDX中使用文件中的字体? [英] How to use a font from a file in SharpDX?

查看:25
本文介绍了如何在SharpDX中使用文件中的字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为 Windows 8 应用程序生成图像,我将使用 SharpDX API

i want to generate image for windows 8 app, i'm going to use SharpDX API

这是我幸运地处理并粘贴的代码示例

here is the code sample which i thankfully coped and paste

private MemoryStream RenderStaticTextToBitmap()
    {
        var width = 400;
        var height = 100;
        var pixelFormat = WicPixelFormat.Format32bppBGR;

        var wicFactory = new ImagingFactory();
        var dddFactory = new SharpDX.Direct2D1.Factory();
        var dwFactory = new SharpDX.DirectWrite.Factory();

        var wicBitmap = new Bitmap(
            wicFactory,
            width,
            height,
            pixelFormat,
            BitmapCreateCacheOption.CacheOnLoad);

        var renderTargetProperties = new RenderTargetProperties(
            RenderTargetType.Default,
            new D2DPixelFormat(Format.Unknown, AlphaMode.Unknown),
            0,
            0,
            RenderTargetUsage.None,
            FeatureLevel.Level_DEFAULT);
        var renderTarget = new WicRenderTarget(
            dddFactory,
            wicBitmap,
            renderTargetProperties)
        {
            TextAntialiasMode = TextAntialiasMode.Cleartype
        };

        renderTarget.BeginDraw();

        var textFormat = new TextFormat(dwFactory, "Consolas", 48)
        {
            TextAlignment = SharpDX.DirectWrite.TextAlignment.Center,
            ParagraphAlignment = ParagraphAlignment.Center
        };
        var textBrush = new SharpDX.Direct2D1.SolidColorBrush(
            renderTarget,
            SharpDX.Colors.Blue);

        renderTarget.Clear(Colors.White);
        renderTarget.DrawText(
            "Hi, mom!",
            textFormat,
            new RectangleF(0, 0, width, height),
            textBrush);

        renderTarget.EndDraw();

        var ms = new MemoryStream();

        var stream = new WICStream(
            wicFactory,
            ms);

        var encoder = new PngBitmapEncoder(wicFactory);
        encoder.Initialize(stream);

        var frameEncoder = new BitmapFrameEncode(encoder);
        frameEncoder.Initialize();
        frameEncoder.SetSize(width, height);
        frameEncoder.PixelFormat = WicPixelFormat.FormatDontCare;
        frameEncoder.WriteSource(wicBitmap);
        frameEncoder.Commit();

        encoder.Commit();

        frameEncoder.Dispose();
        encoder.Dispose();
        stream.Dispose();

        ms.Position = 0;
        return ms;
    }

这与已安装的字体以出色的方式工作....我在资产文件夹中有字体,我想使用 - 我有大约 604 种自定义字体,我动态选择了字体 - ,我知道可以加载文件来自文件夹....帮助请

this working in excellent way with installed fonts .... i have font in the assets folder and i want to use -i have about 604 custom fonts and i chose the font dynamically- , i know there is away to load file from folder .... help plz

推荐答案

不幸的是,遗憾的是,DirectWrite 中没有 API 可以轻松支持这一点.您需要开发自己的字体加载器和相关类.有 SharpDX 示例 CustomFont 正在从资源,因此您可以调整它以从其他位置加载.

Unfortunately, afaik, there is no API in DirectWrite that supports this easily. You need to develop your own font loader and related classes. There is the SharpDX sample CustomFont that is loading fonts from resources, so you could adapt it to load from another location.

这篇关于如何在SharpDX中使用文件中的字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆