c# - Clearing canvas has a delay -


below code simple app draws rectangle on canvas in window , takes screen shot of app using copyfromscreen function when key pressed. before called however, call canvas.children.clear(). expect resultant image not have rectangle in it, does. seems actual rectangle image isn't removed canvas when function called time after.

i tried putting in system.threading.thread.sleep(1000); after clear() call rectangle stays on screen full second well. it's getting removed after key press function finishes, there way remove before copyfromscreen call?

to run need add reference system.drawing.

xaml code

<window x:class="canvastest.mainwindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         title="mainwindow" width="210" height="240"         keydown="keypressed">     <window.background>         <solidcolorbrush color="white"/>     </window.background>     <grid>         <canvas name="canvas"             horizontalalignment="left" verticalalignment="top">         </canvas>     </grid> </window> 

.cs code

using system; using system.componentmodel; using system.windows; using system.windows.controls; using system.windows.shapes;  namespace canvastest {     public partial class mainwindow : window {         public mainwindow() {             initializecomponent();              left = 0;             top = 0;              rectangle rect = new rectangle {                 stroke = system.windows.media.brushes.black,                 strokethickness = 1,                 width = 100,                 height = 100             };              canvas.children.add(rect);             canvas.setleft(rect, 50);             canvas.settop(rect, 50);         }          private void keypressed(object sender, system.windows.input.keyeventargs e) {             system.drawing.bitmap bitmap = new system.drawing.bitmap((int)width, (int)height);              system.drawing.graphics graphics = system.drawing.graphics.fromimage(bitmap);              canvas.children.clear();             graphics.copyfromscreen(0, 0, 0, 0,                                     new system.drawing.size(bitmap.width, bitmap.height),                                     system.drawing.copypixeloperation.sourcecopy);              string path = environment.getfolderpath(environment.specialfolder.desktop);             path += "\\muckleewespic.png";              bitmap.save(path, system.drawing.imaging.imageformat.png);         }     } } 

how can clear canvas , take screen shot without behaviour happening? , no 'don't add rect' isn't solution ha, minimal example of larger app in problem occurring.

thanks.

using in colinsmith's link:

static void waitforrenderpass() {   application.current.dispatcher     .begininvoke( dispatcherpriority.applicationidle, new action( () => {} ) )     .wait(); } 

sort of works waiting on render pass problem it's not possible (afaik) tell if render pass contains want.

in practice depending on system load/video card drivers/... has called multiple times. i've had 1 machine had called in loop 3 times, , each of calls took 2 frames @60hz complete. call above return indicating render thread idle, means changes want have been rendered, in turn means screen captures contain should have. ended using

for( int = 0 ; < 5 ; ++i ) {   waitforrenderpass();   thread.sleep( 10 ); } 

it's ugly , it's hack hasn't failed (yet).

regarding highcore's comment: can capture screen with wpf-only classes well, however:

  • it has same problem op started questiomn in first place won't solve (except not using system.drawing)
  • you won't able main window's chrome in capture
  • it's markedly slower copyscreen
  • it not render exact same pixels rendered screen

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -