1

我想在 foreach 循环中的 PictureBox 上绘制文本。这是负责渲染的代码(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));
      }

}

但遗憾的是,文本没有呈现。如果我注释掉

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

线,它确实有效!我不知道如何让它工作。

我想使用 TextRenderer,但我不知道如何获取控件的 IDeviceContext(我在互联网上看到的所有示例都在 Paint 事件中使用 PaintEventArgs.Graphics)。

此外,如果这是相关的,则 GG PictureBox 是另一个图片框的子项,并且具有透明背景。

我在无效后尝试刷新框,工作代码:

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));
      }

}
4

3 回答 3

2

您修改了图像内容,但 PictureBox 完全没有意识到这一点。您没有重新分配其 Image 属性。您需要告诉它它需要重新绘制屏幕上显示的图像。添加这行代码:

    GG.Invalidate();
于 2014-01-26T13:37:43.280 回答
1

只需绘制 aBitmap并将其显示在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;
于 2014-01-26T13:49:38.707 回答
0
        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;

根据 StevenHouben 的回答,我粘贴了我的 C# 版本。它工作正常。谢谢@StevenHouben。

于 2014-02-25T07:58:45.790 回答