Graphics.DrawString() 与 TextRenderer.DrawText()

2023-12-22

我对这两种方法感到困惑。

我的理解是Graphics.DrawString()使用GDI+并且是基于图形的实现,而TextRenderer.DrawString()使用GDI并且允许大范围的字体并支持unicode。

我的问题是当我尝试将基于十进制的数字作为百分比打印到打印机时。我的研究使我相信 TextRenderer 是更好的方法。

然而,MSDN 建议,“不支持打印 TextRenderer 的 DrawText 方法。您应该始终使用 Graphics 类的 DrawString 方法。”

我使用 Graphics.DrawString 打印的代码是:

if (value != 0)
    e.Graphics.DrawString(String.Format("{0:0.0%}", value), GetFont("Arial", 12, "Regular"), GetBrush("Black"), HorizontalOffset + X, VerticleOffset + Y);

对于 0 到 1 之间的数字打印“100%”,对于小于零的数字打印“-100%”。

当我放置时,

Console.WriteLine(String.Format("{0:0.0%}", value));

在我的打印方法中,该值以正确的格式打印(例如:75.0%),所以我很确定问题出在 Graphics.DrawString() 中。


这似乎与Graphics.DrawString or TextRenderer.DrawString or Console.Writeline.

您提供的格式说明符,{0.0%},不只是简单地附加一个百分号。根据 MSDN 文档here http://msdn.microsoft.com/en-us/library/0c899ak8.aspx#SpecifierPct, the %自定义说明符...

导致数字在格式化之前乘以 100。

在我的测试中,两者Graphics.DrawString and Console.WriteLine当传递相同的值和格式说明符时表现出相同的行为。

Console.WriteLine test:

class Program
{
    static void Main(string[] args)
    {
        double value = .5;
        var fv = string.Format("{0:0.0%}", value);
        Console.WriteLine(fv);
        Console.ReadLine();
    }
}

Graphics.DrawString Test:

public partial class Form1 : Form
{
    private PictureBox box = new PictureBox();

    public Form1()
    {
        InitializeComponent();
        this.Load += new EventHandler(Form1_Load);
    }

    public void Form1_Load(object sender, EventArgs e)
    {
        box.Dock = DockStyle.Fill;
        box.BackColor = Color.White;

        box.Paint += new PaintEventHandler(DrawTest);
        this.Controls.Add(box);
    }

    public void DrawTest(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        double value = .5;
        var fs = string.Format("{0:0.0%}", value);
        var font = new Font("Arial", 12);
        var brush = new SolidBrush(Color.Black);
        var point = new PointF(100.0F, 100.0F);

        g.DrawString(fs, font, brush, point);
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Graphics.DrawString() 与 TextRenderer.DrawText() 的相关文章

  • 使用遗留代码(使用reinterpret_cast)真的是一种很好的技术吗?

    下面的代码来自一篇关于C 面试问题的帖子here https www toptal com c plus plus interview questions 我从来不知道这种技术 尽管它声称是一种很好的技术 我的问题是 什么情况下需要使用它
  • 以编程方式更新 Wifi 网络

    我正在尝试创建一个程序 当某个 wifi 网络在范围内时 该程序会连接到该网络 即使已经连接到另一个 wifi 也是如此 我在用着简单Wifi https github com DigiExam simplewifi 基本上效果很好 除了在
  • 为什么 xcode IDE 认为 `friend` 是保留字

    我一直在开发一个个人项目 并在我创建的新类中包含以下代码 property readonly getter isFriend BOOL friend 它似乎没有任何问题 当我构建它时 它可以编译得很好 但是当我们在xcode IDE看起来像
  • 大小为 k 的非连续子序列的最大值的最小值

    在开始之前 我希望这个问题不是重复的 我发现了几个类似的问题 但它们似乎都没有描述完全相同的问题 但如果它是重复的 我会很高兴看到一个解决方案 即使它与我的算法不同 我一直在尝试回答这个问题 https stackoverflow com
  • 如何在C++中列出Python模块的所有函数名称?

    我有一个 C 程序 我想导入一个 Python 模块并列出该模块中的所有函数名称 我该怎么做 我使用以下代码从模块中获取字典 PyDictObject pDict PyDictObject PyModule GetDict pModule
  • 如何通过分解 y 轴来减小 mschart 的高度

    如何降低 mschart 的高度 如下所示 编辑 就我而言 我不想查看中断图表 this chart1 ChartAreas 0 AxisY ScaleBreakStyle Enabled false 您似乎正在寻找AxisY ScaleB
  • 括号内声明的对象的范围

    如果我声明一个这样的对象 void main myclass objectA anotherclass true true 0 即 我通过直接调用后者的构造函数来创建一个 objectA 和另一个对象 anotherclass anothe
  • ArrayList 有什么问题?

    最近我问了一个关于 SO 的问题 其中提到了可能使用 c ArrayList 来解决问题 有人评论说使用数组列表不好 我想了解更多有关此的信息 我以前从未听说过关于数组列表的这种说法 有人可以带我了解使用数组列表可能出现的性能问题吗 C n
  • 模板类中模板方法专门化的 clang 自动返回类型错误?

    试图理解另一个问题 https stackoverflow com questions 38054055 clang fails to compile template function with auto return type insi
  • 结构大小与 typedef 版本不同?

    我的代码中有以下结构声明和 typedef struct blockHeaderStruct bool allocated unsigned int length typedef struct blockHeaderStruct block
  • 用 std::generate_n 填充 std::map

    我想填写一个std map using std generate n但无法让它发挥作用 我尝试过的是这样的事情 unsigned number of pairs 5 std map
  • 使用 StartServiceCtrlDispatcher 与 StartService 从 C 语言启动 Windows 服务有什么区别?

    我尝试使用 StartServiceCtrlDispatcher 中所述https msdn microsoft com en us library windows desktop bb540475 v vs 85 aspx https m
  • 如何检查日期时间是否发生在今天?

    有没有比下面的代码更好的 net 方法来检查 今天 是否发生了 DateTime if newsStory WhenAdded Day DateTime Now Day newsStory WhenAdded Month DateTime
  • C 中的 2 个字符要短

    我有2个字符 Char 128和查尔2 如何将这些字符转为 Short640 in C 我试过了 unsigned short getShort unsigned char array int offset short returnVal
  • 在 C 中运行 setuid 程序的正确方法

    我有一个权限为4750的进程 我的Linux系统中存在两个用户 root 用户和 appz 用户 该进程继承以 appz 用户身份运行的进程管理器的权限 我有两个基本惯例 void do root void int status statu
  • C# 中的 mshtml.HTMLDocumentClass

    在 C 中 我设法从 InternetExplorer 对象获取整个 HTMLDocumentClass 导航到某个 URL 然而 在 Visual Studio 2008 的调试模式下 该特定 URL 的 HTMLDocumentClas
  • 使用属性和性能

    我正在优化我的代码 我注意到使用属性 甚至自动属性 对执行时间有深远的影响 请参阅下面的示例 Test public void GetterVsField PropertyTest propertyTest new PropertyTest
  • 在for循环中声明和初始化变量

    可以简单写一下吗 for int i 0 代替 int i for i 0 在 C 或 C 中 并且会变量i只能在循环内部访问 它在 C 中有效 它在 C 的原始版本中是不合法的 但在 C99 中被采用为 C 的一部分 当时一些 C 功能被
  • 将二进制长字符串转换为十六进制 C#

    我正在寻找一种将长二进制字符串转换为十六进制字符串的方法 二进制字符串看起来像这样 0110011010010111001001110101011100110100001101101000011001010110001101101011 我
  • 在派生类中访问基类变量

    class Program static void Main string args baseClass obj new baseClass obj intF 5 obj intS 4 child obj1 new child Consol

随机推荐