Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Repaint graphics

Status
Not open for further replies.

01310564

Programmer
Oct 9, 2003
96
GB
Using the graphics class I have created a program that allows people to draw lines and rectangles on a form. The problem occurs when the form is refreshed all the graphics disappear. Is there a way to store the graphics and repaint them when needed?

Cheers,

Hugh
 
There are many ways to do this. The easiest way IMO is to draw your graphics onto a bitmap object in memory, then when the screen is repainted, draw the bitmap each time.
 
I'm quite new to .net so i don't have a clue where to start with this. Some help would be very much apprecated. Cheers,
Hugh
 
If I was doing it I woudn't go with the bitmap...how do u interact with already drawn objects if you want to enable erasing??

Keep all of your lines, rectangles, etc in an array.

Dim myRectangles() as Rectangle

For starters, make you array 100 or something, just to see what it going on. Later down the track, you will need to check your array size against how many rectangles you have and increase the array when it gets nearly full.

On your mouse down event, or however you draw your rectangle, go through your array to find the next empty space and then add your rectangles co-ords into into it.

Then on your paint event, loop through the array drawing each rectangle.

Try that out and you should be able to store anything you draw and then later on, you can write code that will interact with drawn objects.

Make sure you also have in your Load event:

Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or _
ControlStyles.DoubleBuffer _
Or ControlStyles.ResizeRedraw _
Or ControlStyles.UserPaint, True)

This helps with flickering and a few other things on redraws, as long as your drawing direclty onto your form.
 
It sounds like he knows how to draw them on the screen already.

There is not need for an array or collection of drawn objects, unless it is to be used like a typical drawing program as opposed to a paint program.

As for erasing....erasing on MS paint works by going over areas of the screen with an erase button.

But like I said, if it is to be used like a drawing program with no free hand...then it would be a good idea to keep these things in a collection.

But, using a bitmap is still best in both scenarios. Then you can take advantage of double buffering.


As for how to work the bitmap, what you want to do is create a new bitmap and draw onto the bitmap's graphics object.

Dim MyBitmap As New Bitmap(Width, Height)

Then paint your objects to it:

Dim g As Graphics = Graphics.FromImage(MyBitmap)
g.DrawRectangle(.........

Then, on your form's paint event, paint the bitmap

e.Graphics.DrawImage(MyBitmap, 0,0)
 
My work backgroud is to do with drawing programs, ie Cad, not sure if anyone has heard of programs such as 12D and MapInfo (MapInfo is worldwide application though). That is the reason behind my suggestion.

But as the first post wasn't too clear on what his program was for, paint program or cad type of stuff, I thought I would give a little input.

Just out of interests sake, by using your code and methods, how do you erase lines? Do you just paint another object over it using white or whatever the background colour is?
 
I never have much of a need to make a full scale user-drawing type program. Most of the graphics I do are static for custom controls, etc.

But yes, I would use a white or whatever to erase. To tell you the truth, when I first read the post, I didn't notice the drawing rectangles and lines.

BTW, now I am wondering. If you use an array or collection, etc. Then how do you delete a whole drawn object. For example, if you want to remove a rectangle, do you allow the user to sort of "select" the rectangle, then allow for deletion?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top