高质量全屏截图 VB.Net
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10930569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
High Quality Full Screenshots VB.Net
提问by Tony
I'm trying to add a feature to my program to take a full screenshot of the users screen when they click a button. I got the program to take the screenshot and open a file dialog box to save it, the saving works. The issue is that no matter how I save the screenshot, the saved image has significant quality loss and pixelates around text and stuff. This is a massive issue because I need the image to save exactly as it is seen on the users screen, I cannot have ANY quality loss at all. I tried to save the image as a jpg and a png and both gave me quality loss. I was wondering if anyone could point me towards some code or a method that would allow me to save the screenshots at the same quality as the users screen. I would like to save the image as a JPG or a PNG if possible. Any help would greatly be appreciated!
我正在尝试向我的程序添加一项功能,以便在用户单击按钮时截取用户屏幕的完整屏幕截图。我让程序截取屏幕截图并打开一个文件对话框进行保存,保存工作正常。问题是,无论我如何保存屏幕截图,保存的图像都有明显的质量损失和文本和内容周围的像素化。这是一个大问题,因为我需要保存的图像与在用户屏幕上看到的完全一样,我根本不会有任何质量损失。我试图将图像另存为 jpg 和 png,两者都给我带来了质量损失。我想知道是否有人可以向我指出一些代码或方法,使我能够以与用户屏幕相同的质量保存屏幕截图。如果可能,我想将图像保存为 JPG 或 PNG。任何帮助将不胜感激!
回答by Romil Kumar Jain
Get the image in Bitmap format and save it as bmp.
获取Bitmap格式的图片,保存为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
回答by Alex Essilfie
For starters, JPEG images use a lossy compression algorithm so you tend to lose quality when you save in that format. It is preferable to save as Bitmap(BMP), which is uncompressed, or PNG, which uses a lossless compression.
首先,JPEG 图像使用有损压缩算法,因此以该格式保存时质量往往会下降。最好保存为未压缩的位图(BMP) 或使用无损压缩的PNG。
Here is code to copy the working area of the screen to a PNG Image.
这是将屏幕的工作区域复制到 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
回答by Developer
I've found that adding 3 lines to the above code significantly improves the quality of the image
我发现在上面的代码中添加 3 行可以显着提高图像的质量
var graphics = Graphics.FromImage(theRequestedAllocatedImage);
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
// Then call
graphics.CopyFromScreen(..)
回答by rich
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
回答by Romil Kumar Jain
.Net usually saves the file in 96dpi, so using following code you can save the file in higher resolution with Jpeg or other format.
.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