生成OSD文字 - 洛阳翼展电脑


生成OSD文字

2025-10-22 乱云飞

#NoEnv
#SingleInstance Force
SendMode Input
SetWorkingDir, %A_ScriptDir%

; 检查magick.exe是否存在
IfNotExist, magick.exe
{
    MsgBox, 16, 错误, 在同目录下未找到 magick.exe!`n请将 magick.exe 放在脚本同一目录中。
    ExitApp
}

; 创建GUI界面
Gui, Font, s10, Microsoft YaHei
Gui, Add, GroupBox, x10 y10 w380 h60, 图片设置
Gui, Add, Text, x20 y35 w40, 宽度:
Gui, Add, Edit, x60 y32 w60 vImageWidth, 1920
Gui, Add, Text, x130 y35 w40, 高度:
Gui, Add, Edit, x170 y32 w60 vImageHeight, 1080
Gui, Add, Text, x240 y35 w40, 背景:
Gui, Add, DropDownList, x280 y32 w100 vBackground, transparent||white|black|red|green|blue

Gui, Add, GroupBox, x10 y80 w380 h120, 文字设置
Gui, Add, Text, x20 y105 w40, 内容:
Gui, Add, Edit, x60 y102 w320 vTextContent, 测试OK

Gui, Add, Text, x20 y135 w40, X坐标:
Gui, Add, Edit, x60 y132 w60 vPosX, 100
Gui, Add, Text, x130 y135 w40, Y坐标:
Gui, Add, Edit, x170 y132 w60 vPosY, 100
Gui, Add, Text, x240 y135 w40, 字号:
Gui, Add, Edit, x280 y132 w100 vFontSize, 32

Gui, Add, Text, x20 y165 w40, 字体:
Gui, Add, DropDownList, x60 y162 w170 vFontName, 监控专用||微软雅黑|宋体|黑体|楷体|仿宋
Gui, Add, Text, x240 y165 w40, 颜色:
Gui, Add, DropDownList, x280 y162 w100 vTextColor, red||white|black|blue|purple|green|yellow|orange|pink|brown|gray|cyan|magenta

Gui, Add, GroupBox, x10 y210 w380 h60, 输出设置
Gui, Add, Text, x20 y240 w50, 文件名:
Gui, Add, Edit, x70 y237 w210 vOutputFile, 1.png
Gui, Add, Button, x290 y237 w90 gBrowseFile, 浏览...

Gui, Add, Button, x80 y280 w100 h35 gGenerateImage, 生成图片
Gui, Add, Button, x200 y280 w100 h35 gResetSettings, 重置设置
Gui, Add, StatusBar,, 就绪 - 点击"生成图片"开始创建

Gui, Show, w400 h360, OSD生成器
SB_SetText("就绪 - 点击""生成图片""开始创建")
return

; 浏览文件按钮
BrowseFile:
    FileSelectFile, SelectedFile, S16, 1.png, 选择输出文件, 图片文件 (*.png;*.jpg;*.gif;*.bmp)
    if (SelectedFile != "")
    {
        GuiControl,, OutputFile, %SelectedFile%
    }
return

; 生成图片按钮
GenerateImage:
    Gui, Submit, NoHide
    
    ; 检查必要字段
    if (ImageWidth = "" || ImageHeight = "" || TextContent = "" || OutputFile = "")
    {
        SB_SetText("错误:请填写所有必要字段")
        MsgBox, 48, 错误, 请填写所有必要字段!
        return
    }
    
    ; 验证数字输入
    if ImageWidth is not integer
    {
        MsgBox, 48, 错误, 宽度必须是整数!
        return
    }
    if ImageHeight is not integer
    {
        MsgBox, 48, 错误, 高度必须是整数!
        return
    }
    if PosX is not integer
    {
        MsgBox, 48, 错误, X坐标必须是整数!
        return
    }
    if PosY is not integer
    {
        MsgBox, 48, 错误, Y坐标必须是整数!
        return
    }
    if FontSize is not integer
    {
        MsgBox, 48, 错误, 字号必须是整数!
        return
    }
    
    SB_SetText("正在生成图片...")
    
    ; 获取实际字体名称
    ActualFont := GetActualFont(FontName)
    
    ; 构建ImageMagick命令 - 使用相对路径
    MagickCommand := "magick.exe -size " ImageWidth "x" ImageHeight " xc:" Background 
    MagickCommand .= " -font """ ActualFont """"
    MagickCommand .= " -pointsize " FontSize
    MagickCommand .= " -fill " TextColor
    MagickCommand .= " -gravity northwest"
    MagickCommand .= " -annotate +" PosX "+" PosY " """ TextContent """"
    MagickCommand .= " """ OutputFile """"
    
    ; 显示执行的命令(调试用)
    ; MsgBox, % MagickCommand
    
    ; 执行命令
    RunWait, %ComSpec% /c "%MagickCommand%", , Hide, ProcessID
    
    ; 检查是否生成成功
    if FileExist(OutputFile)
    {
        FileGetSize, FileSize, %OutputFile%
        if (FileSize > 0)
        {
            SB_SetText("图片生成成功: " OutputFile)
            MsgBox, 64, 成功, 图片已成功生成!`n`n文件:%OutputFile%`n尺寸:%ImageWidth%x%ImageHeight%`n文字:%TextContent%
            
            ; 询问是否打开图片
            MsgBox, 36, 打开图片, 是否要打开生成的图片?
            IfMsgBox, Yes
            {
                Run, %OutputFile%
            }
        }
        else
        {
            SB_SetText("图片生成失败 - 文件大小为0")
            MsgBox, 16, 错误, 图片生成失败!`n生成的文件大小为0。`n请检查参数设置。
            FileDelete, %OutputFile%
        }
    }
    else
    {
        SB_SetText("图片生成失败")
        MsgBox, 16, 错误, 图片生成失败!`n请检查:`n1. 文字内容是否包含特殊字符`n2. 字体是否可用`n3. 颜色格式是否正确
    }
return

; 获取实际字体名称函数
GetActualFont(FontName)
{
    ; 字体名称映射
    if (FontName = "监控专用")
        return "jkfont.ttf"  ; 直接使用相对路径
    else if (FontName = "宋体")
        return "SimSun-&-NSimSun"
    else if (FontName = "黑体")
        return "SimHei"
    else if (FontName = "楷体")
        return "KaiTi"
    else if (FontName = "仿宋")
        return "FangSong"
    else if (FontName = "微软雅黑")
        return "Microsoft-YaHei-&-Microsoft-YaHei-UI"
    else
        return FontName
}

; 重置设置按钮
ResetSettings:
    GuiControl,, ImageWidth, 1920
    GuiControl,, ImageHeight, 1080
    GuiControl, ChooseString, Background, transparent
    GuiControl,, TextContent, 测试OK
    GuiControl, ChooseString, FontName, 监控专用
    GuiControl,, FontSize, 32
    GuiControl,, PosX, 100
    GuiControl,, PosY, 100
    GuiControl, ChooseString, TextColor, red
    GuiControl,, OutputFile, 1.png
    SB_SetText("设置已重置")
return

; GUI关闭
GuiClose:
GuiEscape:
    ExitApp
本文链接:http://80c.cc/ez/853.html
0

发表评论:

VirtualCamera:在录像机中虚拟一个通道显示电脑桌面、视频文件、USB摄像头~
预ICP备10086-001号 © 翼展网/80C.CC 技术支持/洛阳翼展科技
TEL / 13213610060 QQ / 345794501
Powered by emlog