0

So I have a fairly simple Winforms app. It has a panel with a bmp image that is being updated every second, via

Graphics.DrawImage

I want to be able to draw a rectangle over the updating images. There seems to be no way to do this in Winforms, however.

First, I tried drawing on the first panel, but there seems to be no way to set the z-index of Graphics elements, so every time the image gets updated, the rectangle is deleted.

Second, I tried making a second panel and drawing in it. Yet, it seems like there is no way to make a truly transparent panel in Winforms. I looked around for some workarounds, but it seems like all they do is essentially make the second panel part of the first, so the previous issue with the rectangle getting deleted arises again.

Is there anyway to do what I am trying to do? Without switching to WPF?

Daniel Gorelik
  • 279
  • 1
  • 2
  • 10
  • It should be something in line with paint? http://www.codeproject.com/Articles/26071/Draw-Over-WinForms-Controls – Max Jun 11 '14 at 14:46
  • The paint event is called when the control is redrawn. I want it to be called on the command of a user. – Daniel Gorelik Jun 11 '14 at 14:55
  • Simply put the DrawRectangle call **after** the DrawImage call. – Hans Passant Jun 11 '14 at 14:58
  • _set the z-index of Graphics elements_ There is only one and the place to use it is in the Paint event. So you simply place your DrawRectanlge code after the DrawImage code and you're good. To intitiate a first call to Paint you call panel1.Invalidate(); to keep it alive you probably must store the coordinates somewhere! – TaW Jun 11 '14 at 14:59
  • @HansPassant - theoretically, that does work, but it flickers since the image is being redrawn constantly. Anyway to make the image drawing be permanently under the rectangle drawing? – Daniel Gorelik Jun 11 '14 at 15:06
  • 1
    Then use a [double-buffered panel](http://stackoverflow.com/a/3113515/17034). – Hans Passant Jun 11 '14 at 15:07
  • @TaW - I guess a workaround would be to create a flag that is False by default. Then, when I want to draw the rectangle, I set the flag to true and call panel1.Invalidate(). Then, in the paint function, it only paints if the flag is true. But, that seems very workaroundy. – Daniel Gorelik Jun 11 '14 at 15:09
  • Give the flag a meaningful name and it will be not a workaround at all! – TaW Jun 11 '14 at 15:14

0 Answers0