weixin_51047377
Hello、小太阳
采纳率0%
2021-03-04 15:38

VB将窗体保存为图片并保存

如何通过VB编程将运行后的窗体保存为图片并保存?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • weixin_45629296 The 祺℡ 1月前

    可以是可以,不过需要调用window dll去获取窗口handle。以下VB代码:

    Imports System
    Imports System.Drawing
    Imports System.Drawing.Imaging
    Imports System.Runtime.InteropServices
    
    Namespace MyDll
    	Friend Class MyClass
    		Private Structure RECT
    			Public x1 As Integer
    			Public y1 As Integer
    			Public x2 As Integer
    			Public y2 As Integer
    		End Structure
    
    		Private Shared Declare Function FindWindow Lib "user32.dll" (lpClassName As String, lpWindowName As String) As IntPtr
    
    		Private Shared Declare Function GetWindowDC Lib "user32.dll" (hwnd As IntPtr) As IntPtr
    
    		Private Shared Declare Function GetWindowRect Lib "user32.dll" (hWnd As IntPtr, <Out()> ByRef rect As Win32Helper.RECT) As IntPtr
    
    		Private Shared Declare Function PrintWindow Lib "user32.dll" (hwnd As IntPtr, hdcBlt As IntPtr, nFlags As UInteger) As Boolean
    
    		Private Shared Declare Function CreateCompatibleDC Lib "gdi32.dll" (hdc As IntPtr) As IntPtr
    
    		Private Shared Declare Function CreateCompatibleBitmap Lib "gdi32.dll" (hdc As IntPtr, nWidth As Integer, nHeight As Integer) As IntPtr
    
    		Private Shared Declare Function SelectObject Lib "gdi32.dll" (hdc As IntPtr, hgdiobj As IntPtr) As IntPtr
    
    		Private Shared Declare Function DeleteDC Lib "gdi32.dll" (hdc As IntPtr) As Integer
    
    		Public Sub CaptureWindowToFile(lpWindowName As String, filename As String, format As ImageFormat)
    			Dim expr_07 As IntPtr = Win32Helper.FindWindow(Nothing, lpWindowName)
    			Dim windowDC As IntPtr = Win32Helper.GetWindowDC(expr_07)
    			Dim intPtr As IntPtr = Win32Helper.CreateCompatibleDC(windowDC)
    			Dim rECT As Win32Helper.RECT
    			Win32Helper.GetWindowRect(expr_07, rECT)
    			Dim intPtr2 As IntPtr = Win32Helper.CreateCompatibleBitmap(windowDC, rECT.x2 - rECT.x1, rECT.y2 - rECT.y1)
    			Win32Helper.SelectObject(intPtr, intPtr2)
    			Win32Helper.PrintWindow(expr_07, intPtr, 0U)
    			Dim arg_65_0 As Image = Image.FromHbitmap(intPtr2)
    			Win32Helper.DeleteDC(windowDC)
    			Win32Helper.DeleteDC(intPtr)
    			arg_65_0.Save(filename, format)
    		End Sub
    	End Class
    End Namespace
    
    点赞 评论 复制链接分享