wpf - How to render a checked CheckBox (Aero theme) to a RenderTargetBitmap? -
my checkbox rendered without check mark. if use 1 checkbox (instance object) render can check mark show, cannot use solution. need able render using local checkbox. checkbox has aero theme applied via "/presentationframework.aero;component/themes/aero.normalcolor.xaml" , must have me. other themes built-in themes royal, , luna render check mark.
xaml
<canvas name="canvas"> <button click="button_click" canvas.left="440" canvas.top="277"> paint </button> <image name="image"/> </canvas>
c#
private void button_click(object sender, routedeventargs e) { var checkbox = new checkbox { width = 100, height = 30, ischecked = true, }; var rectangle = new rect(0, 0, checkbox.width, checkbox.height); //need var visualbrush = new visualbrush(checkbox); checkbox.arrange(rectangle); var rendertargetbitmap = new rendertargetbitmap((int)rectangle.width, (int)rectangle.height, 96, 96, pixelformats.default); rendertargetbitmap.render(checkbox); rendertargetbitmap.freeze(); image.source = rendertargetbitmap; }
i used reflector @ content of presentationframework.aero assembly, , found out what's going on here. have solution, you're not going it...
in aero theme, checkboxes rendered bulletchrome
control. control uses animations when switches 1 state another, when render checkbox, animation unchecked checked not complete, , tick mark not yet visible. need wait animation complete...
just putting thread.sleep
before rendering won't work, because while dispatcher thread sleeping, animation stopped. need start new thread wait before rendering checkbox:
private void button_click(object sender, routedeventargs e) { var checkbox = new checkbox { width = 100, height = 30, ischecked = true, }; var rectangle = new rect(0, 0, checkbox.width, checkbox.height); //need var visualbrush = new visualbrush(checkbox); checkbox.arrange(rectangle); new thread(() => { thread.sleep(300); // animation lasts 300ms dispatcher.invoke(() => { var rendertargetbitmap = new rendertargetbitmap((int)rectangle.width, (int)rectangle.height, 96, 96, pixelformats.default); rendertargetbitmap.render(checkbox); rendertargetbitmap.freeze(); image.source = rendertargetbitmap; }); }).start(); }
(this code uses dispatcherextensions.invoke
extension method, need reference system.windows.presentation assembly , import system.windows.threading
namespace)
note animation doesn't occur if systemparameters.clientareaanimation
false. since property readonly, doesn't you...
Comments
Post a Comment