Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Graphics.DrawString不';行不通_C#_Winforms_C# 4.0_Drawstring_Textrenderer - Fatal编程技术网

C# Graphics.DrawString不';行不通

C# Graphics.DrawString不';行不通,c#,winforms,c#-4.0,drawstring,textrenderer,C#,Winforms,C# 4.0,Drawstring,Textrenderer,我想在foreach循环中的PictureBox上绘制文本。这是负责渲染的代码(GG是当前处于循环中的PictureBox) 但遗憾的是,文本没有呈现出来。如果我把这个评论删掉 //((PictureBox)GG).Image = (Image)obj; 行,它确实有用!我不知道如何让它工作 我想使用TextRenderer,但我不知道如何获得控件的IDeviceContext(我在internet上看到的所有示例都使用Paint EventArgs.Graphics in Paint eve

我想在foreach循环中的PictureBox上绘制文本。这是负责渲染的代码(GG是当前处于循环中的PictureBox)

但遗憾的是,文本没有呈现出来。如果我把这个评论删掉

//((PictureBox)GG).Image = (Image)obj;
行,它确实有用!我不知道如何让它工作

我想使用TextRenderer,但我不知道如何获得控件的IDeviceContext(我在internet上看到的所有示例都使用Paint EventArgs.Graphics in Paint event)

此外,如果这是相关的,GG PictureBox是另一个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));
      }

}

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

    GG.Invalidate();

只需绘制一个
位图
,并将其显示在
图片框中

// 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;

根据StevenHouben的回答,我粘贴了我的C版本。它很好用。谢谢@StevenHouben。

我试着在((PictureBox)GG下面添加这个;行,文本闪烁一秒钟,然后消失。解决了,回答问题。嗯,不,在修改图像之前强制重新绘制仍然是错误的。仅使用Invalidate(),使用Refresh()没有任何意义。但它当时不起作用!我检查了所有可能的组合!请注意,
.Refresh()
将立即重新绘制,
.Invalidate()
将在方便时重新绘制(应用程序处于空闲状态)。
// 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;