简体   繁体   中英

How to delete an image from Graphics

Suppose I use DrawImage a few times to draw a bunch of images.

e.Graphics.DrawImage(newImage, destRect);

How can I delete a specific image from the Graphics paper that I drew it on? Is there a specific function I can use for deletion?

I have tried dispose and Rectangle.Empty , but they don't actually delete the image I already drew on the paper.

krikara, what you should do is:

  1. Keep track of all the bricks in a list
  2. When the ball hits a brick, remove that brick from the list

Every frame, you must redraw everything from scratch. This includes the ball, the paddle and the list of bricks (and whatever else you need, like score, etc.)

Hope this clears up whatever confusion you had.

First of all, there's no concept of "delete an object" in GDI+ Graphics. You have to redraw the the entire client area in every frame. You should keep a list of objects and their states in memory and redraw the entire surface in every frame. Beware though, this can lead to flickers and not-so-smooth user experience. Here are a few tips to avoid these:

  1. Make sure your Form or UserControl has its DoubleBuffered property set to True. This will result in a far smoother animation than otherwise.

  2. NEVER EVER call CreateGraphics() to get a reference to the Graphics object in your drawing loop. Update your list of objects and states in your loop and then call Invalidate() on your Control/Form and do the drawing process in Paint event.

  3. One overload of Invalidate() allows you to specify the rectangle that needs to be invalidated (redrawn). You can pass a "safe" rectangle around your bouncing ball's current position (say 20 pixels wider/taller than the ball size) as agrument and then draw only that portion in your Paint event.

  4. To further increase performance you can keep auxilary information such as scores, player names etc. outside the actual "game board" and use normal labels/textboxes for them instead of drawing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM