如何借鉴PictureBox的文本? [英] How to draw text on picturebox?

查看:130
本文介绍了如何借鉴PictureBox的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Google搜索借鉴PictureBox的C#文字,但我无法找到任何东西useful.Then我GOOGLE了关于形式C#绘制文本,我发现了一些代码,但它不工作,我希望它的工作方式。

I googled for "Drawing text on picturebox C#" ,but I couldnt find anything useful.Then I googled for "Drawing text on form C#" and I found some code,but it doesnt work the way I want it to work.

    private void DrawText()
    {
        Graphics grf = this.CreateGraphics();
        try
        {
            grf.Clear(Color.White);
            using (Font myFont = new Font("Arial", 14))
            {
                grf.DrawString("Hello .NET Guide!", myFont, Brushes.Green, new PointF(2, 2));
            }
        }
        finally
        {
            grf.Dispose();
        }
    }

当我调用函数,的背景颜色。形式变成白色(它是黑色的默认设置)

When I call the function,the background color of the form becomes white(it's black by default).

我的问题:

1:请问在这项工作?图片框

1:Will this work on a picturebox?

2:如何来解决这个问题。

2:How to fix the problem?

推荐答案

你不希望调用清除() - 这就是为什么它的转向白色的背景,它会掩盖你的图片

You don't want that call to Clear() - that's why it's turning the background white, and it will cover up your picture.

您要使用的Paint事件在图片框。你从e.Graphics图形引用,然后使用您的样品中有束带()。

You want to use the Paint event in the PictureBox. You get the graphics reference from e.Graphics, and then use the DrawString() that you have in your sample.

下面是一个示例。只是一个图片框添加到窗体,然后添加一个事件处理程序Paint事件:

Here's a sample. Just add a picture box to your form, and add an event handler for the Paint event:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    using (Font myFont = new Font("Arial", 14))
    {
        e.Graphics.DrawString("Hello .NET Guide!", myFont, Brushes.Green, new Point(2, 2));
    }
}



(请注意,你不会看到在文本设计时间 - 你必须运行方案,使油漆)

(Note that you won't see the text at design time - you'll have to run the program for it to paint).

这篇关于如何借鉴PictureBox的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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