Graphics in PictureBox not visible at start

Hi!

Does anybody know, how to make visible graphics in the PictureBox control immediately at start of the program, when the form is loaded and visible

I have UserControl which contains PictureBox and I created procedure that uses GDI+ to draw some lines, texts in PictureBox. It works OK when I run it by clicking button on the form, but when I put the same procedure to the constructor of UserControl or to the event handler which handles Load event (of my UserControl) there is no effect - PictureBox is empty.

Is there any other event I could handle or any other method to draw these graphics without any explicit action from the user



Answer this question

Graphics in PictureBox not visible at start

  • SMang

    IF you're drawing lines, etc, you'd do better to draw them directly onto the window, in a paint event handler. As soon as you move away from the paint event, you're relying on the control to refresh itself properly. You can force this by calling Invalidate(), which forces a paint event.



  • Mike Droney

    It took some time, but finally I solved the problem. The clue was really to put all code that draws my objects to paint evet handler and use e.Graphics as graphic object instead of creating new one for my PictureBox.

    Thanks for help.


  • Lovelu

    Still, there is something I'm doing wrong. When I put my drawing procedure to the Paint event handler, my drawing appears at start, but then immediately disappears. This happens also when I switch to the other window and come back - then my drawing disappears.

    You mentioned, that I should draw directly on window, but I need to manipulate a bit my drawing at run-time: draw additional elements, change scale, coordinate system etc. That's why I want to have separated area for the drawing and I use PictureBox with GDI + functionality for that purpose. In fact it is very simple graphic window. Perhaps you know better way to realise that, but possibly using basic .NET components

    Now, I have to leave my project for a while (1 or 2 weeks), but if you have any ideas I can use them later to solve my problem.


  • Graphics in PictureBox not visible at start