You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VisualBasic实现PictureBox图片添加当前时间戳并保存的技术需求

在VisualBasic中给PictureBox图片添加时间戳后保存

你的现有代码里,注释掉的时间戳绘制逻辑无法生效——因为picCapture.CreateGraphics()是把文字画在控件的显示表层,而非图片数据本身,保存时自然不会带上时间戳。以下是修正后的完整代码,实现将时间戳直接绘制到图片对象上再保存:

Dim data As IDataObject
Dim bmap As Image

' 从剪贴板复制图片
SendMessage(hHwnd, WM_CAP_EDIT_COPY, 0, 0)

' 从剪贴板获取图片并转换为Bitmap
data = Clipboard.GetDataObject()

If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
    bmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Image)

    ' 在图片对象上绘制时间戳(核心修改部分)
    Using g As Graphics = Graphics.FromImage(bmap)
        ' 格式化日期时间为易读格式,可按需调整
        Dim timestampText As String = "SAVED ON - " & Now.ToString("yyyy-MM-dd HH:mm:ss")
        g.DrawString(timestampText, New Font("Arial", 18), Brushes.Yellow, 10, 10)
    End Using ' 自动释放Graphics资源,避免内存泄漏

    ' 更新PictureBox显示带时间戳的图片
    picCapture.Image = bmap
    PictureBox1.Image = picCapture.Image

    sfdImage.FileName = TextBox1.Text
    SendKeys.Send("{Enter}")

    If sfdImage.ShowDialog = DialogResult.OK Then
        ' 根据文件名后缀选择保存格式
        If InStr(sfdImage.FileName, ".bmp") Then
            bmap.Save(sfdImage.FileName, Imaging.ImageFormat.Bmp)
        Else
            bmap.Save(sfdImage.FileName, Imaging.ImageFormat.Jpeg)
            TextBox1.Clear()
            TextBox1.Focus()
        End If
    End If
End If
End Sub

关键修改说明:

  • 用Graphics.FromImage(bmap)获取图片的绘图上下文,确保时间戳直接写入图片数据,而非控件表面
  • 用Using语句包裹Graphics对象,自动释放资源,避免内存泄漏
  • 对日期时间做格式化处理(Now.ToString("yyyy-MM-dd HH:mm:ss")),让时间戳更清晰,格式可按需自定义
  • 移除无效的CreateGraphics()调用,替换为针对图片对象的绘制操作

内容的提问来源于stack exchange,提问作者omar weeka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 11:40:33