Files
IOT_SIM/runtime/Sim.WinForms/Host/PropValue.cs
T
xuyueandClaude Opus 5.5 6b088240da
CI/CD / test (push) Canceled after 0s
CI/CD / deploy (push) Canceled after 0s
项目 4 任务 2 网络摄像机:PictureBox、Image / Bitmap、照片列表
- 摄像头:自绘东围墙场景(Camera/scene.svg),按云台位置切成 640×480 帧(build-frames.ts);
  Pan 每次转一格,红外对射被遮挡时画面出现翻墙的人;类名、委托、事件参数与教材 dll 一致,
  ImageRead 每 250 ms 在后台线程触发
- Image / Bitmap 保存真实图片数据:Save、FromFile、FromStream,文件夹不存在时报 GDI+ 一般性错误;
  图片每张只发给浏览器一次(按内容哈希缓存)
- 程序所在目录(AppDomain.CurrentDomain.BaseDirectory,编译检查单独放行)和当前目录指向可写的 bin/Debug,
  沙箱里在沙箱的临时目录
- PictureBox(SizeMode、BorderStyle);设计器工具箱加"图片框";运行区显示程序保存的图片,点开看大图;
  实训台摄像头图标显示开启状态
- 评分规则加图片框显示图片、摄像头开关与转向、保存图片三种检查;示例 p4-t2-网络摄像机
- 测试:摄像头端到端、图片读写与程序目录、AppDomain 编译检查、评分出错情况

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 20:58:09 +08:00

59 lines
2.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections;
using System.Drawing;
using System.Globalization;
using System.Text.Json.Nodes;
using System.Windows.Forms;
namespace Sim.Host;
/// <summary>把控件属性值转换成发给浏览器的 JSON(格式见 web/src/winforms/format.ts)</summary>
public static class PropValue
{
public static JsonNode? From(object? value) => value switch
{
null => null,
string s => JsonValueOf(s),
bool b => JsonValueOf(b),
char c => JsonValueOf(c == '\0' ? "" : c.ToString()),
int or long or short or byte => JsonValueOf(Convert.ToInt64(value, CultureInfo.InvariantCulture)),
float or double or decimal => JsonValueOf(Convert.ToDouble(value, CultureInfo.InvariantCulture)),
Enum e => JsonValueOf(e.ToString()),
Point p => new JsonObject { ["x"] = p.X, ["y"] = p.Y },
Size s => new JsonObject { ["width"] = s.Width, ["height"] = s.Height },
Color c => ColorCss(c),
Font f => new JsonObject
{
["family"] = f.Name,
["size"] = f.SizeInPoints,
["bold"] = f.Bold,
["italic"] = f.Italic,
["underline"] = f.Underline,
["strikeout"] = f.Strikeout,
},
Image image => new JsonObject { ["key"] = SimHost.ImageKey(image), ["width"] = image.Width, ["height"] = image.Height },
Padding p => new JsonObject { ["left"] = p.Left, ["top"] = p.Top, ["right"] = p.Right, ["bottom"] = p.Bottom },
IEnumerable list => ListOf(list),
_ => JsonValueOf(value.ToString() ?? ""),
};
/// <summary>未设置的颜色(Color.Empty)返回 null,表示沿用父控件的颜色</summary>
public static JsonNode? ColorCss(Color color)
{
if (color.IsEmpty) return null;
if (color.A == 0) return "transparent";
if (color.A < 255)
return $"rgba({color.R},{color.G},{color.B},{(color.A / 255.0).ToString("0.###", CultureInfo.InvariantCulture)})";
return $"#{color.R:x2}{color.G:x2}{color.B:x2}";
}
private static JsonArray ListOf(IEnumerable items)
{
var array = new JsonArray();
foreach (var item in items) array.Add(From(item));
return array;
}
private static JsonNode JsonValueOf<T>(T value) => JsonValue.Create(value)!;
}