我正在尝试向我的程序添加一项功能,以便在用户单击按钮时获取用户屏幕的完整屏幕截图。我得到程序截取屏幕截图并打开文件对话框保存,保存工作。问题在于,无论我如何保存屏幕截图,保存的图像都会有明显的质量损失,并且会在文本和内容周围出现像素化问题。这是一个大问题,因为我需要将图像保存为在用户屏幕上看到的完全保存,我根本不会有任何质量损失。我试图将图像保存为jpg和png,两者都给了我质量损失。我想知道是否有人可以指向我一些代码或方法,允许我以与用户屏幕相同的质量保存屏幕截图。如果可能,我想将图像保存为JPG或PNG。非常感谢任何帮助!
答案 0 :(得分:4)
以位图格式获取图像并将其另存为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
答案 1 :(得分:2)
对于初学者来说,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
答案 2 :(得分: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
答案 3 :(得分:1)
我发现在上面的代码中添加3行可以显着提高图像的质量
var graphics = Graphics.FromImage(theRequestedAllocatedImage);
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
// Then call
graphics.CopyFromScreen(..)
答案 4 :(得分: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