简体   繁体   English

Graphics.DrawString不起作用

[英]Graphics.DrawString doesn't work

I want to draw a text over a PictureBox in a foreach loop. 我想在foreach循环中的PictureBox上绘制文本。 This is the code that is responsible for rendering(GG is a PictureBox that is currently in the loop) 这是负责渲染的代码(GG是当前在循环中的PictureBox)

if (GG != null)
      {
            ((PictureBox)GG).Image = (Image)obj;
            using (Graphics g = ((PictureBox)GG).CreateGraphics()) {
            g.DrawString(i["amount"].ToString(), kryptonRichTextBox1.Font, 
            new SolidBrush(Color.Gold), new Point(16, 18));
      }

}

But sadly, the text is not rendered. 但遗憾的是,文本没有呈现。 If I comment out the 如果我评论出来的话

//((PictureBox)GG).Image = (Image)obj;

line, it does work! 线,它确实有效! I have no idea how to get it to work. 我不知道如何让它工作。

I wanted to use the TextRenderer, but I don't know how to get an IDeviceContext of a control(and all examples I see on the internet use PaintEventArgs.Graphics in Paint event). 我想使用TextRenderer,但我不知道如何获取控件的IDeviceContext(我在互联网上看到的所有示例都使用Paint事件中的PaintEventArgs.Graphics)。

Also, if this is relevant, the GG PictureBox is a child of another picturebox, and has a transparent background. 此外,如果这是相关的,GG PictureBox是另一个图片框的孩子,并具有透明背景。

I tried to refresh the box after invalidating, the working code: 我在尝试无效后刷新了工具代码:

if (GG != null)
      {
            ((PictureBox)GG).Image = (Image)obj;
            ((PictureBox)GG).Invalidate();
            ((PictureBox)GG).Refresh();
            using (Graphics g = ((PictureBox)GG).CreateGraphics()) {
            g.DrawString(i["amount"].ToString(), kryptonRichTextBox1.Font, 
            new SolidBrush(Color.Gold), new Point(16, 18));
      }

}

You modified the image content but the PictureBox is completely unaware of that. 您修改了图像内容,但PictureBox完全没有意识到这一点。 You didn't reassign its Image property. 您没有重新分配其Image属性。 You will need to tell it that it needs to redraw the image as displayed on the screen. 您需要告诉它需要重绘屏幕上显示的图像。 Add this line of code: 添加以下代码行:

    GG.Invalidate();

Just draw on a Bitmap and show it in the PictureBox : 只需在Bitmap上绘制并在PictureBox显示它:

// A new bitmap with the same size as the PictureBox
var bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);

//Get the graphics objectm which we can use to draw
var graphics = Graphics.FromImage(bitmap);

//Draw stuff
graphics.DrawString(i["amount"].ToString(), kryptonRichTextBox1.Font, 
        new SolidBrush(Color.Gold), new Point(16, 18));

//Show the bitmap with graphics image in the PictureBox
pictureBox.Image = bitmap;
        Image digidashboard = new Bitmap(Properties.Resources.digidashboard);
        //using (Graphics g = ((PictureBox)pictureBoxDashboard).CreateGraphics())
        //{
        //    g.DrawString("80.00", this.Font, new SolidBrush(Color.Red), 3, 6);
        //    pictureBoxUnlock.Image = digidashboard;
        //    pictureBoxDashboard.Invalidate();
        //}
        Graphics g = Graphics.FromImage(digidashboard);
        g.DrawString("80.00", this.Font, new SolidBrush(Color.Red), 3, 6);
        pictureBoxDashboard.Image = digidashboard;

According to StevenHouben's answer, I paste my C# version. 根据StevenHouben的回答,我粘贴了我的C#版本。 It works fine. 它工作正常。 Thanks @StevenHouben. 谢谢@StevenHouben。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM