Title: Improve the jigsaw puzzle program in C#
The original jigsaw puzzle program works pretty well, but it takes a little while when you click on a tile and there are a large number of tiles. In that situation, the program spends a second or so building the background image because copying all of those images into position takes a while.
This version overcomes that problem. The program needs to to generate a new image under two circumstances: when you click on a tile to move it, and when you drop a tile that you are moving.
In either case, the program is only moving one tile. The improved program only redraws the area around that tile.
The program makes a Rectangle representing the position of the tile. It expands the rectangle slightly to allow for the thick border drawn around it and then calls the Graphics object's SetClip method to restrict drawing to that Rectangle. that allows the graphics subsystem to skip drawing anything that doesn't overlap the Rectangle, and that saves a huge amount of time.
If you run the two versions of the program and compare performance, you'll see a big difference.
The moral of the story is that SetClip can sometimes speed up drawing if you need to redraw a small part of a complex picture.
Download the example to experiment with it and to see additional details.
|