c# - Drawn shape in Picturebox doesn't clear -
i trying show train moves on map .so let me explain method ,i draw map on picturebox map,and trains on picturebox train,i put train picturebox on map picturebox .
more details:https://stackoverflow.com/a/9158849/2538037
so use 2 function here :
public void drawmap() { var graph = graphics.fromimage(map); list<point> lstpointleft = new list<point>(); foreach (var t in lstsensorleft) { point objpoint = new point(t.xlocation, t.ylocation); lstpointleft.add(objpoint); rectangle rectsens = new rectangle(t.xlocation, t.ylocation, 3, 3); try { graph.fillrectangle(whitebrush, rectsens); } catch (exception ea) { } if (t.stationid != null) { rectangle rectehsansq = new rectangle(t.xlocation - 6, t.ylocation - 6, 12, 12); graph.fillrectangle(bluebrush, rectehsansq); graph.drawstring(objstationrepository.findby(i => i.id == t.stationid).first().name, pictureboxmetromap.font, brushes.white, t.xlocation +40, t.ylocation +50); } } list<point> lstpointright = new list<point>(); foreach (var t in lstsensorright) { point objpoint = new point(t.xlocation + 30, t.ylocation + 30); lstpointright.add(objpoint); rectangle rectsens = new rectangle(t.xlocation + 30, t.ylocation + 30, 3, 3); graph.fillrectangle(whitebrush, rectsens); if (t.stationid != null) { rectangle rectposition = new rectangle(t.xlocation + 24, t.ylocation + 24, 12, 12); graph.fillrectangle(bluebrush, rectposition); graph.drawstring(objstationrepository.findby(i => i.id == t.stationid).first().name, pictureboxmetromap.font, brushes.white, t.xlocation - 50, t.ylocation - 30); } } graph.drawlines(pline, lstpointleft.toarray()); graph.drawlines(pline, lstpointright.toarray()); pictureboxmetromap.image = map; } this function draws map ,and function draws trains on picturebox:
public void drawonlinetrain() { var graph = graphics.fromimage(map); if (onlinetrainlist.count > 0) { foreach (onlinetrain t in onlinetrainlist.tolist()) { // graph.dispose(); rectangle recttrainstate = new rectangle(t.xtrainlocation.value - 3, t.ytrainlocation.value - 3, 7, 7); graph.fillrectangle(redbrush, recttrainstate); } } pictureboxonlinetrain.image = map; } so use thread update train picturebox ,i call thread in form_load :
private void frmmain_load(object sender, eventargs e) { pictureboxonlinetrain.parent = pictureboxmetromap; map= new bitmap(pictureboxmetromap.size.width, pictureboxmetromap.size.height); updatelistbox = new updatelistboxdelegate(this.updatestatus); // initialise , start worker thread workerthread = new thread(new threadstart(this.getonlinetrain)); workerthread.start(); } so in thread start method gets location of online train:
public void getonlinetrain() { while(true) { onlinetrainrepository objonlinetrainrepository = new onlinetrainrepository(); onlinetrainlist = objonlinetrainrepository.getall().tolist(); objonlinetrainrepository = null; invoke(updatelistbox); } } here start updatelistbox draw train:
private void updatestatus() { foreach (onlinetrain onlinetrain in onlinetrainlist.tolist()) { lstlog.items.add("train id=" + onlinetrain.trainid + " | current x position=" + onlinetrain.xtrainlocation + " | current y position=" + onlinetrain.ytrainlocation); pictureboxonlinetrain.image = null; drawonlinetrain(); } } as can see here show movement have clear old location of trains ,and using :
pictureboxonlinetrain.image = null; but doesn't work ,and every rectangle remain on screen ?!!!
best regards
1. address problem directly
- you never clear bitmap! note drawing on top of there when using
graphics.fromimage - you use 1 , same
bitmapobject drawing. (in combination previous point) have entire "scene" inmaptime - no need have severalpictureboxes in case! - be careful: if
pictureboxrefresehes while you're drawing, unfinished state visible! manipulating image shown.
2. do
- render 1 buffer (you using
bitmapfine, maybe consider usingbufferedgraphics) - render control whenever (or it's
paintevent fires) instead of usingpicturebox
Comments
Post a Comment