4

我正在尝试向我的程序添加一项功能,以便在用户单击按钮时对用户屏幕进行完整截图。我让程序截取屏幕截图并打开一个文件对话框来保存它,保存工作。问题是,无论我如何保存屏幕截图,保存的图像都会有明显的质量损失和文本和内容周围的像素化。这是一个大问题,因为我需要完全按照用户屏幕上所见的方式保存图像,我根本不会有任何质量损失。我试图将图像保存为 jpg 和 png 格式,但都给了我质量损失。我想知道是否有人可以向我指出一些代码或方法,使我能够以与用户屏幕相同的质量保存屏幕截图。如果可能,我想将图像保存为 JPG 或 PNG。任何帮助将不胜感激!

4

5 回答 5

6

获取位图格式的图像并将其保存为 bmp。

Private Function TakeScreenShot() As Bitmap

    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)

    Return screenGrab

End Function
于 2012-06-07T11:10:28.903 回答
3

对于初学者来说,JPEG 图像使用有损压缩算法,因此当您以该格式保存时,您往往会损失质量。最好保存为未压缩的位图(BMP) 或使用无损压缩的PNG 。

这是将屏幕的工作区域复制到 PNG 图像的代码。

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    'the working area excludes all docked toolbars like taskbar, etc.
    Dim currentScreen = Screen.FromHandle(Me.Handle).WorkingArea

    'create a bitmap of the working area
    Using bmp As New Bitmap(currentScreen.Width, currentScreen.Height)

        'copy the screen to the image
        Using g = Graphics.FromImage(bmp)
            g.CopyFromScreen(New Point(0, 0), New Point(0, 0), currentScreen.Size)
        End Using

        'save the image
        Using sfd As New SaveFileDialog() With {.Filter = "PNG Image|*.png",
                                                .InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop}

            If sfd.ShowDialog() = Windows.Forms.DialogResult.OK Then
                bmp.Save(sfd.FileName, System.Drawing.Imaging.ImageFormat.Png)
            End If
        End Using
    End Using
End Sub
于 2012-06-07T13:34:31.430 回答
1

我发现在上面的代码中添加 3 行可以显着提高图像的质量

var graphics = Graphics.FromImage(theRequestedAllocatedImage);

graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;


// Then call
graphics.CopyFromScreen(..)
于 2013-01-02T14:20:17.290 回答
1

.Net 通常将文件保存为 96dpi,因此使用以下代码可以将文件保存为 Jpeg 或其他格式的更高分辨率。

'Create a new bitmap
Using Bmp As New Bitmap(800, 1000, Imaging.PixelFormat.Format32bppPArgb)
'Set the resolution to 300 DPI
    Bmp.SetResolution(300, 300)
'Create a graphics object from the bitmap
    Using G = Graphics.FromImage(Bmp)
'Paint the canvas white
        G.Clear(Color.White)
'Set various modes to higher quality
        G.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
        G.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
        G.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias

'Create a font
        Using F As New Font("Arial", 12)
'Create a brush
            Using B As New SolidBrush(Color.Black)
'Draw some text
                G.DrawString("Hello world", F, B, 20, 20)
            End Using
        End Using
    End Using

'Save the file as a TIFF
    Bmp.Save("c:\\test.Jpeg", Imaging.ImageFormat.Jpeg)
End Using
于 2012-06-07T11:32:45.907 回答
1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    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)

    PictureBox1.Image = screenGrab
    PictureBox1.Image.Save("c:\picture.bmp")
End Sub
于 2017-08-21T00:59:21.967 回答