11using System ;
22using System . Collections . Generic ;
3+ using System . IO ;
34using System . Linq ;
45using System . Text ;
5- using System . Windows . Input ;
66using System . Windows ;
7+ using System . Windows . Controls ;
8+ using System . Windows . Documents ;
9+ using System . Windows . Input ;
10+ using System . Windows . Markup ;
11+ using System . Windows . Media ;
12+ using System . Windows . Media . Imaging ;
13+ using System . Xml ;
14+
15+ using Microsoft . Win32 ;
716
817namespace ICSharpCode . XamlDesigner
918{
@@ -13,6 +22,8 @@ public partial class MainWindow
1322 public static SimpleCommand SaveAllCommand = new SimpleCommand ( "Save All" , ModifierKeys . Control | ModifierKeys . Shift , Key . S ) ;
1423 public static SimpleCommand ExitCommand = new SimpleCommand ( "Exit" ) ;
1524 public static SimpleCommand RefreshCommand = new SimpleCommand ( "Refresh" , Key . F5 ) ;
25+ public static SimpleCommand RunCommand = new SimpleCommand ( "Run" , ModifierKeys . Shift , Key . F5 ) ;
26+ public static SimpleCommand RenderToBitmapCommand = new SimpleCommand ( "Render to Bitmap" ) ;
1627
1728 static void RenameCommands ( )
1829 {
@@ -59,6 +70,72 @@ void SaveAllCommand_Executed(object sender, ExecutedRoutedEventArgs e)
5970 {
6071 Shell . Instance . SaveAll ( ) ;
6172 }
73+
74+ void RunCommand_Executed ( object sender , ExecutedRoutedEventArgs e )
75+ {
76+ StringBuilder sb = new StringBuilder ( ) ;
77+ var xmlWriter = XmlWriter . Create ( new StringWriter ( sb ) ) ;
78+ Shell . Instance . CurrentDocument . DesignSurface . SaveDesigner ( xmlWriter ) ;
79+
80+ var txt = sb . ToString ( ) ;
81+ var xmlReader = XmlReader . Create ( new StringReader ( txt ) ) ;
82+
83+ var ctl = XamlReader . Load ( xmlReader ) ;
84+
85+ Window wnd = ctl as Window ;
86+ if ( wnd == null ) {
87+ wnd = new Window ( ) ;
88+ wnd . Content = ctl ;
89+ }
90+ wnd . Show ( ) ;
91+ }
92+
93+ void RenderToBitmapCommand_Executed ( object sender , ExecutedRoutedEventArgs e )
94+ {
95+ int desiredWidth = 300 ;
96+ int desiredHeight = 300 ;
97+
98+ StringBuilder sb = new StringBuilder ( ) ;
99+ var xmlWriter = XmlWriter . Create ( new StringWriter ( sb ) ) ;
100+ Shell . Instance . CurrentDocument . DesignSurface . SaveDesigner ( xmlWriter ) ;
101+
102+ var txt = sb . ToString ( ) ;
103+ var xmlReader = XmlReader . Create ( new StringReader ( txt ) ) ;
104+
105+ var ctl = XamlReader . Load ( xmlReader ) as Control ;
106+ if ( ctl is Window ) {
107+ var wnd = ctl as Window ;
108+ wnd . Width = desiredWidth ;
109+ wnd . Height = desiredHeight ;
110+ wnd . Top = - 10000 ;
111+ wnd . Left = - 10000 ;
112+ wnd . Show ( ) ;
113+ } else {
114+ ctl . Measure ( new Size ( desiredWidth , desiredHeight ) ) ;
115+ ctl . Arrange ( new Rect ( new Size ( desiredWidth , desiredHeight ) ) ) ;
116+ }
117+
118+ RenderTargetBitmap bmp = new RenderTargetBitmap ( 300 , 300 , 96 , 96 , PixelFormats . Default ) ;
119+ bmp . Render ( ctl ) ;
120+
121+ var encoder = new PngBitmapEncoder ( ) ;
122+
123+ encoder . Frames . Add ( BitmapFrame . Create ( bmp ) ) ;
124+
125+ var dlg = new SaveFileDialog ( ) ;
126+ dlg . Filter = "*.png|*.png" ;
127+ if ( dlg . ShowDialog ( ) == true ) {
128+ using ( Stream stm = File . OpenWrite ( dlg . FileName ) ) {
129+ encoder . Save ( stm ) ;
130+ stm . Flush ( ) ;
131+ }
132+ }
133+
134+ if ( ctl is Window ) {
135+ var wnd = ctl as Window ;
136+ wnd . Close ( ) ;
137+ }
138+ }
62139
63140 void ExitCommand_Executed ( object sender , ExecutedRoutedEventArgs e )
64141 {
0 commit comments