vb.net - 以较低的分辨率和图像质量保存屏幕截图

vb.net - Save screenshot with reduced resolution and image quality

我正在使用此代码在生产机器上截取屏幕截图并将它们保存到文件夹中。每个文件的大小约为 400 kb,我真的不需要很好的分辨率和质量。我怎样才能将分辨率减半并降低 jpg 质量以节省 space?

Private Sub TakeScreenShot()

    Dim screenSize As Size = New Size(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
    Dim screenGrab As New Bitmap(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
    Dim g As Graphics = Graphics.FromImage(screenGrab)
    g.CopyFromScreen(New Point(0, 0), New Point(0, 0), screenSize)

    screenGrab.Save("screenshots\test.jpg", Imaging.ImageFormat.Jpeg)

End Sub

跟随this MSDN article,你可以像这样设置50%质量

Private Sub TakeScreenShot()
    Dim screenSize As Size = New Size(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
    Dim screenGrab As New Bitmap(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
    Dim g As Graphics = Graphics.FromImage(screenGrab)
    g.CopyFromScreen(New Point(0, 0), New Point(0, 0), screenSize)
    Dim jpgEncoder As System.Drawing.Imaging.ImageCodecInfo = GetEncoder(System.Drawing.Imaging.ImageFormat.Jpeg)
    Dim myEncoder As System.Drawing.Imaging.Encoder = System.Drawing.Imaging.Encoder.Quality
    Dim myEncoderParameters As New Imaging.EncoderParameters(1)
    Dim myEncoderParameter As New System.Drawing.Imaging.EncoderParameter(myEncoder, 50&)
    myEncoderParameters.Param(0) = myEncoderParameter
    screenGrab.Save("screenshots\test.jpg", jpgEncoder, myEncoderParameters)
End Sub
Public Function GetEncoder(ByVal format As ImageFormat) As ImageCodecInfo
    Dim codecs As ImageCodecInfo() = ImageCodecInfo.GetImageDecoders()
    Dim codec As ImageCodecInfo
    For Each codec In codecs
        If codec.FormatID = format.Guid Then
            Return codec
        End If
    Next codec
    Return Nothing
End Function