簡體   English   中英

VB.NET 2019 - 如何創建和保存當前表單的 bitmap 或 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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM