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

に描画して、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;
于 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 に答える