0

This is a continuation to the previous question posted here:

How to draw line and select it in Panel

The answer is very well put together and comprehensive. However, the user states:

@TaW: In fact you shouldn't ever use CreateGraphics in the first place, as the Graphics object will never stay in scope and the graphics it produces will not persist (i.e. survive a Minimize-maximize sequence)..

Is there a way to use Graphics to the extent that it stays on the panel after minimising/maximising/resizing a window? OR is there an alternative way of doing this other than using Graphics object?

Community
  • 1
  • 1
Romy
  • 407
  • 6
  • 25
  • 3
    Always use the control's `Paint` event. You get a graphics object for free if you do. The key point in that answer is the use of the `panel1_Paint()` method to call the `Draw()` method. – DonBoitnott Jun 07 '16 at 11:07
  • 2
    @Don is right. Please also note: The `Graphics` object does not __contain__ any graphics; it is a **tool** that lets you draw onto a related bitmap, including a control's surface. The system needs to draw all the controls' surfaces at times you can't control; therefore all you want to add to those surfaces must be created from the one event that the system will call, which is the `Paint` event. Only __non-persistent__ graphics operation like displaying a dynamic rubber-band rectangle are ok with a `Graphics` object you get from `control.CreateGraphics()`. And measurements without drawing... – TaW Jun 07 '16 at 11:32
  • 1
    The one alternative would be to draw __into a Bitmap__. Here you can take your time and even cache the `Graphics` object, altough it makes little or no sense..I have linked to an answer that explains the difference between drawing __into a Bitmap__ and __onto a Control__.. – TaW Jun 07 '16 at 11:41
  • Damn, you're right, both of ya. I didn't use Paint event, which is why it got deleted. Thank you both, and I'll look the Bitmap answer also, since now I got to figure out how to keep the image ratio when resizing the window. – Romy Jun 07 '16 at 11:55

0 Answers0