The essence of the problem is this: I have an array of objects. Each object has a numeric value and a PictureBox component (it is created dynamically for each object). Each PictureBox has a picture on the background, which is set in the constructor.

 this.Card.Image = Image.FromFile("Card.png"); 

When I click on the PictureBox I want it to display another picture, and on top of it, it will display the pixel value corresponding to this object. I have the code:

 public void OpenCard() { this.Card.Image = Image.FromFile("CardOpen.png"); this.Card.BackColor = Color.Transparent; Graphics g = Graphics.FromHwnd(this.Card.Handle); g.DrawString(this.Value.ToString(), new Font("Arial", 35), Brushes.Blue, new Point(0, 20)); g.Dispose(); } 

However, when pressed, the picture changes, but there is no number image. No one can help me solve this problem?

  • Try Graphics g = this.Card.CreateGraphics (); - Specter
  • I tried - the result is the same - Pavel Voevoda
  • Maybe the whole thing in this.Card.Image . this.Card.Image . this.Card.Image ? Maybe it was worth putting this picture as BackGround? - Specter
  • I do not know. A numeric value is displayed on top of the current picture, but not on top of a new one. I tried it through BackGround - it didn't work out) - Pavel Voevoda

1 answer 1

I can not understand why to set the Image property and BackColor on the image, especially in the value of Transparent? Well, okay, and the answer is very simple * You draw the number in the wrong place, because the number is first drawn, after the end of your procedure, the interface starts to update according to the specified properties and your graphics are simply erased. There are many options, here is one of them, for your case:


  Image myTempImage = Image.FromFile("CardOpen.png"); Graphics g = Graphics.FromImage(myTempImage); g.DrawString(this.Value.ToString(), new Font("Arial", 35), Brushes.Blue, new Point(0, 20)); g.Dispose(); this.Card.Image = myTempImage; 

I hope I deserve + :)

  • thank. now it all worked! - Pavel Voevoda