VB.NET 2019 - 如何创建和保存当前窗体的位图或jpeg文件图像?

VB.NET 2019 - How to Create and Save a bitmap or jpeg file image of the current form?

我有一个使用单一表单的应用程序,允许用户操作用于计算各种参数的设置。所有这些都有效,直到我开始想要;

  1. 想要保存该计算的图像(即表格图像)或
  2. 打印出来。

我找到了各种解决方案,但大多数使用 C# 代码而不是 VB.NET,那些使用 VB.Net 的解决方案似乎创建了 类,这让我感到困惑。

基本上我可以在表单中添加一个按钮,我想在按下后使用 (btnXYZ.Visible=false) 隐藏它,然后继续生成一个我可以保存的图像文件。

有人可以帮忙吗? 谢谢。

试试这个:

Private Function GetSnapShot() As Bitmap
    Using image As Image = New Bitmap(Me.Width - 10, Me.Height - 10)
        Using graphics As Graphics = graphics.FromImage(image)
            graphics.CopyFromScreen(New Point(Me.Left + 5, Me.Top + 5), Point.Empty, New Size(Me.Width - 10, Me.Height - 10))
        End Using
        Return New Bitmap(SetBorder(image, Color.Black, 1))
    End Using
End Function

Private Function SetBorder(ByVal srcImg As Image, ByVal color As Color, ByVal width As Integer) As Image
    Dim dstImg As Image = srcImg.Clone()
    Dim g As Graphics = Graphics.FromImage(dstImg)
    Dim pBorder As Pen = New Pen(color, width)

    pBorder.Alignment = Drawing2D.PenAlignment.Center
    g.DrawRectangle(pBorder, 0, 0, dstImg.Width - 1, dstImg.Height - 1)
    pBorder.Dispose()
    g.Save()
    g.Dispose()

    Return dstImg
End Function

代码的使用方法如下:

GetSnapShot().Save("File Path goes here where you want to save the image")