using System; using System.IO; using System.Windows; using System.Windows.Media; using System.Windows.Media.Imaging; using ClassNotify.Client.ViewModels; using ClassNotify.Client.Views; namespace ScreenSnapshotTool; public class Program { [STAThread] public static int Main(string[] args) { if (args.Length < 2) { Console.WriteLine("Usage:"); Console.WriteLine(" ScreenSnapshotTool standby "); Console.WriteLine(" ScreenSnapshotTool notification <message> <countdown> <width> <height> <dpi> <outPath>"); return 1; } System.Windows.Media.RenderOptions.ProcessRenderMode = System.Windows.Interop.RenderMode.SoftwareOnly; if (Application.Current == null) { var app = new Application(); app.ShutdownMode = ShutdownMode.OnExplicitShutdown; } string mode = args[0].ToLowerInvariant(); try { if (mode == "standby") { var stateStr = args[1]; var code = args.Length > 2 ? args[2] : ""; if (code == "-" || code == "NONE" || code == "EMPTY") code = ""; int teachers = args.Length > 3 ? int.Parse(args[3]) : 0; int width = args.Length > 4 ? int.Parse(args[4]) : 1920; int height = args.Length > 5 ? int.Parse(args[5]) : 1080; double dpi = args.Length > 6 ? double.Parse(args[6]) : 96.0; string outPath = args.Length > 7 ? args[7] : "standby.png"; var state = Enum.Parse<StandbyUiState>(stateStr, ignoreCase: true); RenderStandby(state, code, teachers, width, height, dpi, outPath); return 0; } else if (mode == "notification") { var title = args[1]; var message = args.Length > 2 ? args[2] : "通知内容"; int countdown = args.Length > 3 ? int.Parse(args[3]) : 15; int width = args.Length > 4 ? int.Parse(args[4]) : 1920; int height = args.Length > 5 ? int.Parse(args[5]) : 1080; double dpi = args.Length > 6 ? double.Parse(args[6]) : 96.0; string outPath = args.Length > 7 ? args[7] : "notification.png"; RenderNotification(title, message, countdown, width, height, dpi, outPath); return 0; } else { Console.WriteLine($"Unknown mode: {mode}"); return 1; } } catch (Exception ex) { Console.Error.WriteLine($"Rendering failed: {ex}"); return 1; } } private static void RenderStandby(StandbyUiState state, string code, int teacherCount, int width, int height, double dpi, string outPath) { var view = new StandbyOverlayView(); var vm = new StandbyOverlayViewModel(); vm.SetState(state, code, 600, teacherCount); view.DataContext = vm; double scale = dpi / 96.0; double dipWidth = width / scale; double dipHeight = height / scale; view.Width = dipWidth; view.Height = dipHeight; if (view.Content is FrameworkElement contentEl) { contentEl.Width = dipWidth; contentEl.Height = dipHeight; contentEl.DataContext = vm; } view.Background = new SolidColorBrush(Color.FromRgb(15, 23, 42)); // Slate-900 view.Measure(new Size(dipWidth, dipHeight)); view.Arrange(new Rect(0, 0, dipWidth, dipHeight)); view.UpdateLayout(); var rtb = new RenderTargetBitmap(width, height, dpi, dpi, PixelFormats.Pbgra32); rtb.Render(view); SaveAndValidateBitmap(rtb, width, height, outPath); Console.WriteLine($"[Standby Rendered] State={state}, Code={code}, Teachers={teacherCount} -> {outPath} ({width}x{height} @ {dpi} DPI)"); } private static void RenderNotification(string title, string message, int countdown, int width, int height, double dpi, string outPath) { // Render full desktop with notification dialog centered on classroom desktop var container = new System.Windows.Controls.Grid { Background = new SolidColorBrush(Color.FromRgb(15, 23, 42)) // classroom background }; var card = new System.Windows.Controls.Border { Width = 900, Height = 560, CornerRadius = new CornerRadius(16), Background = Brushes.White, ClipToBounds = true, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, Effect = new System.Windows.Media.Effects.DropShadowEffect { BlurRadius = 30, ShadowDepth = 10, Opacity = 0.4 } }; var grid = new System.Windows.Controls.Grid(); grid.RowDefinitions.Add(new System.Windows.Controls.RowDefinition { Height = new GridLength(84) }); grid.RowDefinitions.Add(new System.Windows.Controls.RowDefinition { Height = new GridLength(1, GridUnitType.Star) }); grid.RowDefinitions.Add(new System.Windows.Controls.RowDefinition { Height = new GridLength(112) }); // Header var header = new System.Windows.Controls.Border { Background = new SolidColorBrush(Color.FromRgb(37, 99, 235)), // #2563EB Padding = new Thickness(32, 0, 32, 0) }; var headerStack = new System.Windows.Controls.StackPanel { Orientation = System.Windows.Controls.Orientation.Horizontal, VerticalAlignment = VerticalAlignment.Center }; headerStack.Children.Add(new System.Windows.Controls.TextBlock { Text = "🔔", FontSize = 38, VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(0, 0, 16, 0) }); headerStack.Children.Add(new System.Windows.Controls.TextBlock { Text = title, FontSize = 30, FontWeight = FontWeights.Bold, Foreground = Brushes.White, VerticalAlignment = VerticalAlignment.Center }); header.Child = headerStack; System.Windows.Controls.Grid.SetRow(header, 0); grid.Children.Add(header); // Body var body = new System.Windows.Controls.Grid { Background = new SolidColorBrush(Color.FromRgb(248, 250, 252)), Margin = new Thickness(32, 10, 32, 10) }; body.Children.Add(new System.Windows.Controls.TextBlock { Text = message, FontSize = 42, FontWeight = FontWeights.Bold, Foreground = new SolidColorBrush(Color.FromRgb(15, 23, 42)), TextWrapping = TextWrapping.Wrap, TextAlignment = TextAlignment.Center, VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Stretch, Margin = new Thickness(16, 8, 16, 8) }); System.Windows.Controls.Grid.SetRow(body, 1); grid.Children.Add(body); // Footer var footer = new System.Windows.Controls.Border { Background = new SolidColorBrush(Color.FromRgb(248, 250, 252)) }; var footerStack = new System.Windows.Controls.StackPanel { VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center }; var btn = new System.Windows.Controls.Button { Content = "我知道了", FontSize = 24, FontWeight = FontWeights.Bold, Width = 320, Height = 52, Background = new SolidColorBrush(Color.FromRgb(37, 99, 235)), Foreground = Brushes.White, BorderThickness = new Thickness(0) }; footerStack.Children.Add(btn); footerStack.Children.Add(new System.Windows.Controls.TextBlock { Text = $"将在 {countdown} 秒后自动关闭", FontSize = 16, Foreground = new SolidColorBrush(Color.FromRgb(100, 116, 139)), HorizontalAlignment = HorizontalAlignment.Center, Margin = new Thickness(0, 8, 0, 0) }); footer.Child = footerStack; System.Windows.Controls.Grid.SetRow(footer, 2); grid.Children.Add(footer); card.Child = grid; container.Children.Add(card); double scale = dpi / 96.0; double dipWidth = width / scale; double dipHeight = height / scale; container.Width = dipWidth; container.Height = dipHeight; container.Measure(new Size(dipWidth, dipHeight)); container.Arrange(new Rect(0, 0, dipWidth, dipHeight)); container.UpdateLayout(); var rtb = new RenderTargetBitmap(width, height, dpi, dpi, PixelFormats.Pbgra32); rtb.Render(container); SaveAndValidateBitmap(rtb, width, height, outPath); Console.WriteLine($"[Notification Rendered] Title='{title}', Message='{message}' -> {outPath} ({width}x{height} @ {dpi} DPI)"); } private static void SaveAndValidateBitmap(RenderTargetBitmap rtb, int width, int height, string outPath) { int stride = width * 4; byte[] pixels = new byte[height * stride]; rtb.CopyPixels(pixels, stride, 0); long nonTransparentCount = 0; for (int i = 3; i < pixels.Length; i += 4) { if (pixels[i] > 10) nonTransparentCount++; } double coverage = (double)nonTransparentCount / (width * height); if (coverage < 0.05) { throw new Exception($"Visual Render Sanity Failed: Non-transparent pixel coverage is only {coverage:P2} (minimum required 5%)!"); } var dir = Path.GetDirectoryName(Path.GetFullPath(outPath)); if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir)) { Directory.CreateDirectory(dir); } var encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(rtb)); using (var fs = File.Open(outPath, FileMode.Create, FileAccess.Write)) { encoder.Save(fs); } } }