Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 显示上下文菜单并将其屏幕截图转换为图像_C#_.net_Vb.net_Winforms - Fatal编程技术网

C# 显示上下文菜单并将其屏幕截图转换为图像

C# 显示上下文菜单并将其屏幕截图转换为图像,c#,.net,vb.net,winforms,C#,.net,Vb.net,Winforms,我想显示上下文菜单,仅拍摄上下文菜单的屏幕截图,将屏幕截图转换为图像,并隐藏菜单。有人能告诉我怎么做吗 请注意:它是一个ContextMenu,而不是ContextMenuStrip将上下文菜单添加到表单中,然后使用以下代码: Public Class Form3 Public Sub New() InitializeComponent() Me.ContextMenuStrip = ContextMenuStrip1 End Sub

我想显示
上下文菜单
,仅拍摄上下文菜单的屏幕截图,将屏幕截图转换为
图像
,并隐藏菜单。有人能告诉我怎么做吗


请注意:它是一个
ContextMenu
,而不是
ContextMenuStrip
将上下文菜单添加到表单中,然后使用以下代码:

Public Class Form3

    Public Sub New()

        InitializeComponent()
        Me.ContextMenuStrip = ContextMenuStrip1

    End Sub

    Private Sub ContextMenuStrip1_Opened(ByVal sender As Object, ByVal e As System.EventArgs) Handles ContextMenuStrip1.Opened
        JSsetTimeout.SetTimeout(AddressOf PrintScreen, 600)
    End Sub

    Private Sub PrintScreen()
        Dim sc As New ScreenShot.ScreenCapture()
        Dim img As Image = sc.CaptureScreen
        img.Save(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "screenShot.png"), Imaging.ImageFormat.Png)
        JSsetTimeout.SetTimeout(AddressOf HideContextMenu, 600)
    End Sub

    Private Sub HideContextMenu()
        ContextMenuStrip1.Hide()
    End Sub

End Class
您将需要两个类JSsetTimeout屏幕截图

Public Class JSsetTimeout

    Dim res As Object = Nothing
    Dim WithEvents tm As Timer = Nothing
    Dim _obj As Threading.ThreadStart
    Dim _args() As Object

    Public Shared Sub SetTimeout(ByVal obj As Threading.ThreadStart, ByVal TimeSpan As Integer) ', ByVal ParamArray args() As Object)
        Dim jssto As New JSsetTimeout(obj, TimeSpan) ', args)
    End Sub

    Public Sub New(ByVal obj As Threading.ThreadStart, ByVal TimeSpan As Integer, ByVal ParamArray args() As Object)
        If obj IsNot Nothing Then
            _obj = obj
            _args = args
            tm = New Timer With {.Interval = TimeSpan, .Enabled = False}
            AddHandler tm.Tick, AddressOf tm_Tick
            tm.Start()
        End If
    End Sub

    Private Sub tm_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tm.Tick
        tm.Stop()
        If _obj IsNot Nothing Then
            res = _obj.DynamicInvoke(_args)
        Else
            res = Nothing
        End If
    End Sub
End Class

Imports System
Imports System.Runtime.InteropServices
Imports System.Drawing
Imports System.Drawing.Imaging


Namespace ScreenShot
    '/ Provides functions to capture the entire screen, or a particular window, and save it to a file.
    Public Class ScreenCapture
        '/ Creates an Image object containing a screen shot of the entire desktop
        Public Function CaptureScreen() As Image
            Return CaptureWindow(User32.GetDesktopWindow())
        End Function 'CaptureScreen
        '/ Creates an Image object containing a screen shot of a specific window
        Public Function CaptureWindow(ByVal handle As IntPtr) As Image
            Dim SRCCOPY As Integer = &HCC0020
            ' get te hDC of the target window
            Dim hdcSrc As IntPtr = User32.GetWindowDC(handle)
            ' get the size
            Dim windowRect As New User32.RECT
            User32.GetWindowRect(handle, windowRect)
            Dim width As Integer = windowRect.right - windowRect.left
            Dim height As Integer = windowRect.bottom - windowRect.top
            ' create a device context we can copy to
            Dim hdcDest As IntPtr = GDI32.CreateCompatibleDC(hdcSrc)
            ' create a bitmap we can copy it to,
            ' using GetDeviceCaps to get the width/height
            Dim hBitmap As IntPtr = GDI32.CreateCompatibleBitmap(hdcSrc, width, height)
            ' select the bitmap object
            Dim hOld As IntPtr = GDI32.SelectObject(hdcDest, hBitmap)
            ' bitblt over
            GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, SRCCOPY)
            ' restore selection
            GDI32.SelectObject(hdcDest, hOld)
            ' clean up 
            GDI32.DeleteDC(hdcDest)
            User32.ReleaseDC(handle, hdcSrc)

            ' get a .NET image object for it
            Dim img As Image = Image.FromHbitmap(hBitmap)
            ' free up the Bitmap object
            GDI32.DeleteObject(hBitmap)

            Return img
        End Function 'CaptureWindow
        '/ Captures a screen shot of a specific window, and saves it to a file
        Public Sub CaptureWindowToFile(ByVal handle As IntPtr, ByVal filename As String, ByVal format As ImageFormat)
            Dim img As Image = CaptureWindow(handle)
            img.Save(filename, format)
        End Sub 'CaptureWindowToFile
        '/ Captures a screen shot of the entire desktop, and saves it to a file
        Public Sub CaptureScreenToFile(ByVal filename As String, ByVal format As ImageFormat)
            Dim img As Image = CaptureScreen()
            img.Save(filename, format)
        End Sub 'CaptureScreenToFile
        Public Function CaptureDeskTopRectangle(ByVal CapRect As Rectangle, ByVal CapRectWidth As Integer, ByVal CapRectHeight As Integer) As Bitmap
            '/ Returns BitMap of the region of the desktop, similar to CaptureWindow, but can be used to 
            '/ create a snapshot of the desktop when no handle is present, by passing in a rectangle 
            '/ Grabs snapshot of entire desktop, then crops it using the passed in rectangle's coordinates
            Dim SC As New ScreenShot.ScreenCapture
            Dim bmpImage As New Bitmap(sc.CaptureScreen)
            Dim bmpCrop As New Bitmap(CapRectWidth, CapRectHeight, bmpImage.PixelFormat)
            Dim recCrop As New Rectangle(CapRect.X, CapRect.Y, CapRectWidth, CapRectHeight)
            Dim gphCrop As Graphics = Graphics.FromImage(bmpCrop)
            Dim recDest As New Rectangle(0, 0, CapRectWidth, CapRectHeight)
            gphCrop.DrawImage(bmpImage, recDest, recCrop.X, recCrop.Y, recCrop.Width, _
              recCrop.Height, GraphicsUnit.Pixel)
            Return bmpCrop
        End Function
        '/ Helper class containing Gdi32 API functions
        Private Class GDI32
            Public SRCCOPY As Integer = &HCC0020
            ' BitBlt dwRop parameter
            Declare Function BitBlt Lib "gdi32.dll" ( _
                ByVal hDestDC As IntPtr, _
                ByVal x As Int32, _
                ByVal y As Int32, _
                ByVal nWidth As Int32, _
                ByVal nHeight As Int32, _
                ByVal hSrcDC As IntPtr, _
                ByVal xSrc As Int32, _
                ByVal ySrc As Int32, _
                ByVal dwRop As Int32) As Int32

            Declare Function CreateCompatibleBitmap Lib "gdi32.dll" ( _
                ByVal hdc As IntPtr, _
                ByVal nWidth As Int32, _
                ByVal nHeight As Int32) As IntPtr

            Declare Function CreateCompatibleDC Lib "gdi32.dll" ( _
                ByVal hdc As IntPtr) As IntPtr

            Declare Function DeleteDC Lib "gdi32.dll" ( _
                ByVal hdc As IntPtr) As Int32

            Declare Function DeleteObject Lib "gdi32.dll" ( _
                ByVal hObject As IntPtr) As Int32

            Declare Function SelectObject Lib "gdi32.dll" ( _
                ByVal hdc As IntPtr, _
                ByVal hObject As IntPtr) As IntPtr
        End Class 'GDI32
        '/ Helper class containing User32 API functions
        Public Class User32
            <StructLayout(LayoutKind.Sequential)> _
            Public Structure RECT
                Public left As Integer
                Public top As Integer
                Public right As Integer
                Public bottom As Integer
            End Structure 'RECT

            Declare Function GetDesktopWindow Lib "user32.dll" () As IntPtr

            Declare Function GetWindowDC Lib "user32.dll" ( _
                ByVal hwnd As IntPtr) As IntPtr

            Declare Function ReleaseDC Lib "user32.dll" ( _
                ByVal hwnd As IntPtr, _
                ByVal hdc As IntPtr) As Int32

            Declare Function GetWindowRect Lib "user32.dll" ( _
                ByVal hwnd As IntPtr, _
                ByRef lpRect As RECT) As Int32

        End Class 'User32
    End Class 'ScreenCapture 
End Namespace 'ScreenShot
导入系统
导入System.Runtime.InteropServices
导入系统。绘图
导入System.Drawing.Imaging
名称空间屏幕截图
“/提供了捕获整个屏幕或特定窗口并将其保存到文件中的函数。
公共类截图
“/创建包含整个桌面屏幕快照的图像对象
公共函数CaptureScreen()作为图像
返回CaptureWindow(User32.GetDesktopWindow())
“结束函数”捕获屏幕
“/创建包含特定窗口的屏幕快照的图像对象
公共函数CaptureWindow(ByVal句柄作为IntPtr)作为图像
Dim SRCCOPY As Integer=&HCC0020
'获取目标窗口的te hDC
Dim hdcSrc作为IntPtr=User32.GetWindowDC(句柄)
“知道尺寸了吗
将windowRect设置为新用户32.RECT
User32.GetWindowRect(句柄,windowRect)
调宽为整数=windowRect.right-windowRect.left
调高为整数=windowRect.bottom-windowRect.top
'创建可复制到的设备上下文
Dim hdcDest As IntPtr=GDI32.CreateCompatibleDC(hdcSrc)
'创建一个我们可以复制到的位图,
'使用GetDeviceCaps获取宽度/高度
作为IntPtr=GDI32的尺寸hBitmap。CreateCompatibleBitmap(HDCSC、宽度、高度)
'选择位图对象
尺寸保持为IntPtr=GDI32。选择对象(hdcDest、hBitmap)
“完了
GDI32.BitBlt(hdcDest、0、0、宽度、高度、hdcSrc、0、0、SRCCOPY)
'恢复选择
GDI32.选择对象(hdcDest,保持)
“清理
GDI32.DeleteDC(hdcDest)
User32.ReleaseDC(句柄,hdcSrc)
'获取它的.NET映像对象
作为图像的调暗图像=图像。来自hBitmap(hBitmap)
'释放位图对象
GDI32.DeleteObject(hBitmap)
返回img
结束函数“CaptureWindow”
“/捕获特定窗口的屏幕快照,并将其保存到文件中
Public Sub CaptureWindowToFile(ByVal句柄为IntPtr,ByVal文件名为字符串,ByVal格式为ImageFormat)
将图像调暗为图像=捕获窗口(手柄)
保存(文件名、格式)
结束子“CaptureWindowToFile”
“/捕获整个桌面的屏幕截图,并将其保存到文件中
Public Sub CaptureScreentToFile(ByVal文件名为字符串,ByVal格式为ImageFormat)
图像尺寸img=CaptureScreen()
保存(文件名、格式)
End Sub’CaptureScreen文件
公共函数CaptureDeskTopRectangle(ByVal CapRect为矩形,ByVal CapRectWidth为整数,ByVal CapRectHeight为整数)作为位图
“/返回桌面区域的位图,类似于CaptureWindow,但可用于
“/通过传入矩形,在不存在句柄时创建桌面的快照
“/获取整个桌面的快照,然后使用传入的矩形坐标裁剪它
Dim SC作为新屏幕截图。屏幕截图
将BMP图像变暗为新位图(sc.CaptureScreen)
将bmpCrop调整为新位图(CapRectWidth、CapRectHeight、bmpImage.PixelFormat)
Dim recCrop作为新矩形(CapRect.X、CapRect.Y、CapRectWidth、CapRectHeight)
Dim gphCrop As Graphics=Graphics.FromImage(bmpCrop)
Dim recDest作为新矩形(0,0,CapRectWidth,CapRectHeight)
gphCrop.DrawImage(bmp图像、recDest、recCrop.X、recCrop.Y、recCrop.Width、_
recCrop.Height,GraphicsUnit.Pixel)
返回bmpCrop
端函数
“/Helper类,包含Gdi32 API函数
私有类GDI32
Public SRCCOPY As Integer=&hcc020
'BitBlt dwRop参数
声明函数BitBlt Lib“gdi32.dll”(_
ByVal hDestDC作为IntPtr_
ByVal x作为Int32_
ByVal y作为Int32_
ByVal nWidth作为Int32_
ByVal nHeight作为Int32_
ByVal hSrcDC作为IntPtr_
ByVal xSrc作为Int32_
ByVal ySrc作为Int32_
ByVal dwRop作为Int32)作为Int32
声明函数CreateCompatibleBitmap Lib“gdi32.dll”(_
ByVal hdc作为IntPtr_
ByVal nWidth作为Int32_
ByVal nHeight As Int32)As IntPtr
声明函数CreateCompatibleDC Lib“gdi32.dll”(_
ByVal hdc As IntPtr)As IntPtr
声明函数deleteddc Lib“gdi32.dll”(_
ByVal hdc As IntPtr)As Int32
声明函数DeleteObject Lib“gdi32.dll”(_
ByVal hObject As IntPtr)As Int32
声明函数SelectObject Lib“gdi32.dll”(_
ByVal hdc作为IntPtr_
ByVal hObject As IntPtr)As IntPtr
“最终类”GDI32
“/Helper类,包含User32 API函数
公共类用户32
_
公共结构
公共左整数
作为整数的公共top
作为整数的公权
公共底部为整数
端结构的RECT
将函数GetDesktopWindow库“user32.dll”()声明为IntPtr
宣福